简体   繁体   中英

Preserve escaped characters when reading csv with pandas read_csv

I'm reading in a csv file like this:

label, text
a, 'here\'s some text\nwith a new line'
b, 'and some more\nwith a new line'

I'm currently reading it in like this:

df = pd.read_csv(file, quotechar="'", escapechar="\\")

The data frame is created with the text including just a 'n' character where the \n is supposed to be.

'here's some textnwith a new line'
'and some morenwith a new line'

How do I preserve other escaped characters, like \n, when I'm reading in a csv to a dataframe?

You can always just replace \'s after reading the CSV:

df['text'] = df['text'].str.replace(r"\\\'s", "'s", regex=True)
print(df)

  label                                  text
0     a   'here's some text\nwith a new line'
1     b      'and some more\nwith a new line'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM