[英]Preserve escaped characters when reading csv with pandas read_csv
I'm reading in a csv file like this:我正在阅读这样的 csv 文件:
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.数据框是使用文本创建的,其中仅包含一个“n”字符,其中
\n
应该是。
'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?当我从 csv 读取到 dataframe 时,如何保留其他转义字符,例如 \n?
You can always just replace \'s after reading the CSV:阅读 CSV 后,您可以随时替换 \'s:
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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.