9
votes

Trying to convert a .tsv to a .csv. This:

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)

Gives me this error:

  File "tsv2csv.py", line 11, in <module>
    cw.writerows(filecontents)
_csv.Error: need to escape, but no escapechar set
5
Why don't you simply do a find and replace from tab to comma on the file content ? fileContent = re.sub("(?ism)\t", ",", fileContent ) - Pedro Lobito
@PedroLobito Damn, ninja'd! Also, You might need to change quotechar='' to quotechar='"'. - The name's Bob. MS Bob.
I would refer you to this answer stackoverflow.com/questions/2535255/… Does this help? - dparadis28
@PedroLobito There might be commas in the tsv. - Don Roby
@hannah, which escape character did you chose? - Pedro Lobito

5 Answers

6
votes

While attempting to write to the CSV file, it encounters a token where it has to insert an escape character. However, you have not defined one.

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.

Source: https://docs.python.org/2/library/csv.html#csv.Dialect.escapechar

Example code:

# 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)
9
votes
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)

We can use the above code to convert the .tsv file to .csv file

1
votes

TSV is a file type where fields are separated by tab. If you want to convert a TSV into a CSV (comma separated value) you just need to do a find and replace from TAB to COMMA.

Update:
As pointed out by 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.

i.e.:

import re
tsv = open('tsv.tsv', 'r')
fileContent =  tsv.read()
appDesc = re.sub("""(?ism)(,|"|')""", r"\\\1", 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()
0
votes
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)
0
votes
import pandas as pd
file_path = "/DataS1_interactome.tsv"
DataS1_interactome.csv = pd.read_csv(file_path, sep="\t")