如何将 .tsv 转换为 .csv?

How do I convert a .tsv to .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 文件时,它遇到了一个必须插入转义字符的标记。但是,你还没有定义。

Dialect.escapechar

A one-character string used by the writer to escape the delimiter if quoting is set to QUOTE_NONE and the quotechar if doublequote is False. On reading, the escapechar removes any special meaning from the following character. It defaults to None, which disables escaping.

来源: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 是一种文件类型,其中字段由制表符分隔。 如果您想将 TSV 转换为 CSV(逗号分隔值),您只需执行查找并将 TAB 替换为 COMMA.

更新:
正如 don-roby, "There might be commas in the tsv", for that we use a regex to escape all the csv special characters as defines by rfc4180.

所指出的

即:

import re
tsv = open('tsv.tsv', 'r')
fileContent =  tsv.read()
appDesc = re.sub("""(?ism)(,|"|')""", r"\", appDesc) # escape all especial charaters (" ' ,) rfc4180
fileContent = re.sub("\t", ",", fileContent) # convert from tab to comma
csv_file = open("csv.csv", "w")
csv_file.write(fileContent)
csv_file.close()
import sys
import csv

tabin = csv.reader(open('sample.txt'), dialect=csv.excel_tab)
commaout = csv.writer(open('sample.csv', 'wb'), dialect=csv.excel)

for row in tabin:
  commaout.writerow(row)
import pandas as pd 
tsv_file='name.tsv'
csv_table=pd.read_table(tsv_file,sep='\t')
csv_table.to_csv('new_name.csv',index=False)

我们可以使用上面的代码将.tsv文件转换为.csv文件

import pandas as pd
file_path = "/DataS1_interactome.tsv"
DataS1_interactome.csv = pd.read_csv(file_path, sep="\t")