繁体   English   中英

使用 pandas read_csv 读取 csv 时保留转义字符

[英]Preserve escaped characters when reading csv with pandas read_csv

我正在阅读这样的 csv 文件:

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

我目前正在这样阅读它:

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

数据框是使用文本创建的,其中仅包含一个“n”字符,其中\n应该是。

'这是一些带有新行的文本'
'还有一些新线'

当我从 csv 读取到 dataframe 时,如何保留其他转义字符,例如 \n?

阅读 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.

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