Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove brackets from python string?

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.

like image 794
hamzah Avatar asked Apr 05 '15 15:04

hamzah


People also ask

How do you remove brackets from a string in Python?

In python, we can remove brackets with the help of regular expressions. # pattern is the special RE expression for finding the brackets.

How do you remove extra brackets from a string?

Approach: Start traversing from left to right. Check if the element at current index is an opening bracket '(' then print that bracket and increment count. Check if the element at current index is a closing bracket ')' and if the count is not equal to zero then print it and decrement the count.

What does {} brackets mean in Python?

What do {} bracket mean in Python? [] brackets are used for lists. List contents can be changed, unlike tuple content. {} are used to define a dictionary in a “list” called a literal.


1 Answers

you can also you replace to just replace the text/symbol that you don't want with the empty string.

text = ["blbal","test"]
strippedText = str(text).replace('[','').replace(']','').replace('\'','').replace('\"','')
print(strippedText)
like image 59
Nikunj Kakadiya Avatar answered Sep 24 '22 18:09

Nikunj Kakadiya