How to read csv with separator inside json?

I would like to read a csv file but the separator sometimes appear in the second column (json). Is it possible to escape pipe when it appears inside quotes ?

from io import StringIO
import pandas as pd

data = 'col1|{"a":"1","b":"2|3","c":"4"}'

df = pd.read_csv(
        StringIO(data),
        header=None,
        sep='|',      
        quoting=csv.QUOTE_NONE,
        quotechar='"',
        doublequote=False
)

Current

0 1 2
col1 {“a”:”1″,”b”:”2 3″,”c”:”4″}

Expected

0 1
col1 {“a”:”1″,”b”:”2|3″,”c”:”4″}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try this:

data = """col1|'{"a":"1","b":"2|3","c":"4"}'"""
df = pd.read_csv(
        StringIO(data),
        header=None,
        sep='|',      
        quotechar="'"
)

pandas can recognize the value as a whole string if the value is enclosed in the specified quotechar, for that you need to surround the json like string in ''.

Also I triple-quoted the data string to preserve the single quotes.
You can also write a simple file with the same string in a csv and try to read_csv with quotechar = "'"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x