本文介绍了如何从python字符串中删除括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从标题知道,您可能会认为这是重复的,但并非如此.

I know from the title you might think that this is a duplicate but it's not.

for id,row in enumerate(rows):
    columns = row.findall("td")

    teamName = columns[0].find("a").text, # Lag
    playedGames = columns[1].text, # S
    wins = columns[2].text,
    draw = columns[3].text,
    lost = columns[4].text,
    dif = columns[6].text, # GM-IM
    points = columns[7].text, # P - last column

    dict[divisionName].update({id :{"teamName":teamName, "playedGames":playedGames, "wins":wins, "draw":draw, "lost":lost, "dif":dif, "points":points }})

这是我的Python代码的样子.大部分代码已删除,但本质上我是从网站中提取一些信息.我将信息保存为字典.当我打印字典时,每个值周围都有一个括号["blbal"],这会在我的Iphone应用程序中引起麻烦.我知道我可以将变量转换为字符串,但是我想知道是否有一种方法可以直接将信息作为字符串获取.

This is how my Python code looks like. Most of the code is removed but essentially i am extracting some information from a website. And i am saving the information as a dictionary. When i print the dictionary every value has a bracket around them ["blbal"] which causes trouble in my Iphone application. I know that i can convert the variables to strings but i want to know if there is a way to get the information DIRECTLY as a string.

推荐答案

列表中似乎有一个字符串:

That looks like you have a string inside a list:

["blbal"] 

要获取字符串,只需索引l = ["blbal"] print(l[0]) -> "blbal".

To get the string just index l = ["blbal"] print(l[0]) -> "blbal".

如果是字符串,请使用str.strip '["blbal"]'.strip("[]")或切片'["blbal"]'[1:-1](如果它们始终存在).

If it is a string use str.strip '["blbal"]'.strip("[]") or slicing '["blbal"]'[1:-1] if they are always present.

这篇关于如何从python字符串中删除括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 21:11