如何将.tsv转换为.csv?
试图将.tsv转换为.csv。这个:
import csv# read tab-delimited file
with open('DataS1_interactome.tsv','rb') as fin:
cr = csv.reader(fin, delimiter='\t')
filecontents = [line for line in cr]
# write comma-delimited file (comma is the default delimiter)
with open('interactome.csv','wb') as fou:
cw = csv.writer(fou, quotechar='', quoting=csv.QUOTE_NONE)
cw.writerows(filecontents)
给我这个错误:
File "tsv2csv.py", line 11, in <module> cw.writerows(filecontents)
_csv.Error: need to escape, but no escapechar set
回答:
尝试写入CSV文件时,它遇到一个必须在其中插入转义字符的令牌。但是,您尚未定义一个。
如果将引号设置为QUOTE_NONE,则编写器使用一个单字符字符串来转义分隔符,如果双引号设置为False,则使用quotechar。阅读时,escapechar从以下字符中删除任何特殊含义。默认为无,这将禁用转义。
来源:https
:
//docs.python.org/2/library/csv.html#csv.Dialect.escapechar
示例代码:
# write comma-delimited file (comma is the default delimiter)with open('interactome.csv','wb') as fou:
cw = csv.writer(fou, quotechar='', quoting=csv.QUOTE_NONE, escapechar='\\')
cw.writerows(filecontents)
以上是 如何将.tsv转换为.csv? 的全部内容, 来源链接: utcz.com/qa/411549.html