How to remove brackets from python string?

你离开我真会死。 提交于 2019-12-21 10:58:08

问题


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 }})

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.


回答1:


That looks like you have a string inside a list:

["blbal"] 

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

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



来源:https://stackoverflow.com/questions/29459008/how-to-remove-brackets-from-python-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!