簡體   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