使用带escapechar的Pandas将csv转换为tsv

2024-10-01 19:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个示例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"

Tags: 文件csvthe代码import示例hellodf
1条回答
网友
1楼 · 发布于 2024-10-01 19:27:06

在阅读csv时使用escapechar对我来说很有效。但它在转换为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

相关问题 更多 >

    热门问题