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

时间:2020-06-13 21:29:01

标签: python pandas

我正在读取这样的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读取到数据帧时,如何保留其他转义字符,例如\ n?

1 个答案:

答案 0 :(得分:0)

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