python - 如何从python字符串中删除括号?

标签 python python-2.7 xpath lxml

我从标题中知道您可能认为这是重复的,但事实并非如此。

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 应用程序出现问题。我知道我可以将变量转换为字符串,但我想知道是否有办法直接将信息作为字符串获取。

最佳答案

看起来你在列表中有一个字符串:

["blbal"] 

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

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

关于python - 如何从python字符串中删除括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29459008/

相关文章:

python - 如何在 Django CMS 中复制 PlaceholderField

python - 条件分配

php - GIS面积计算

python - 即使使用 nohup 子进程也会被杀死

python - 使用 Python 将 CSV 元素从列合并到行

python - 做字谜组的更好方法

python-2.7 - pandas:按二级索引范围对 MultiIndex DataFrame 进行切片

python - Scrapy 选择器返回页面上的所有内容而不是相对的

xml - 从 xml 文档中获取不同的命名空间

java - JAVA中如何获取XPath查询结果的父节点