Home > Software engineering >  pandas - can't strip '}' by str.strip function
pandas - can't strip '}' by str.strip function

Time:12-14

i have below sample data in csv file.

string,class,id
"{""key1"":""value1"":""key2"":""value2""}",classA,idA
"{""key3"":""value3"":""key4"":""value4""}",classB,idB
...

when I use strip function to strip the right brace }, it fails to strip.

df.string =  df.string.apply(lambda x: x.strip('}'))

but when i tried to strip the left brace {, it works.

what's the root cause here?

CodePudding user response:

Do you read the file with doublequote=False, this should work fine with the default doublequote=True?

import io

data = '''string,class,id
"{""key1"":""value1"":""key2"":""value2""}",classA,idA
"{""key3"":""value3"":""key4"":""value4""}",classB,idB'''

df = pd.read_csv(io.StringIO(data), sep=',', doublequote=True)

df['string'] = df['string'].str.strip('}')

print(df)

Output:

                             string   class   id
0  {"key1":"value1":"key2":"value2"  classA  idA
1  {"key3":"value3":"key4":"value4"  classB  idB
  • Related