5

我有一个示例 csv 文件包含

col1
"hello \n
world"
"the quick \njump
\n \r \t brown fox"

示例代码转换为 tsv

import pandas as pd
df = read_csv(r'a.csv')

df.to_csv('data.tsv', sep='\t', encoding='utf-8', escapechar='\n')

预期结果将是

col1
"hello \n world"
"the quick \njump \n \r \t brown fox"

但结果会是

col1
"hello \n
world"
"the quick \njump
\n \r \t brown fox"
4

1 回答 1

5

escapechar在阅读 csv 时使用对我有用。但它在转换为 tsv 时会跳过双引号。

df = pd.read_csv(r'a.csv', escapechar='\n')

df.to_csv('data.tsv', sep='\t', encoding='utf-8', index=False)

输出:

col1
hello \nworld
the quick \njump\n \r \t brown fox
于 2020-07-10T07:58:28.597 回答