How do I make pandas read_csv ignore separator between partially quoted values?

Viewed 47

How do I make read_csv ignore the separator between the double quotes, i.e., ignore the , inside key:"2,3".

Using Python 3.7.4, I have tried the following code:

import pandas as pd
import io

temp=u"""
key:"1",value:"a"
key:"2,3",value:"b"
"""
df = pd.read_csv(io.StringIO(temp), header=None, names=['key', 'value'])

print(df)

This code gives me the following output:

       key      value
0  key:"1"  value:"a"
1   key:"2         3"

What I want to achieve is this:

         key      value
0    key:"1"  value:"a"
1  key:"2,3"  value:"b"
1 Answers

If you really want to do this with read_csv you can do it by using sep='",':

import pandas as pd
import io

temp=u"""
key:"1",value:"a"
key:"2,3",value:"b"
"""
df = pd.read_csv(io.StringIO(temp), header=None, sep='",', names=['key', 'value'], engine='python')
df['key'] = df['key'] + '"'

Result:

key value
0 key:"1" value:"a"
1 key:"2,3" value:"b"
Related