Skip to content

Instantly share code, notes, and snippets.

@Kaufisch
Created May 26, 2019 16:11
Show Gist options
  • Save Kaufisch/68bb40866df2b433a0eac611c926cfe1 to your computer and use it in GitHub Desktop.
Save Kaufisch/68bb40866df2b433a0eac611c926cfe1 to your computer and use it in GitHub Desktop.
Python convert .tsv to .csv file - Including Commas
import io
import re
def convert(file_name):
# Open csv file
csv = io.open(file_name + ".csv", mode="w", encoding="utf-8")
# Convert file_name.tsv to file_name.csv
with io.open(file_name + ".tsv", mode="r", encoding="utf-8") as tsv:
for line in tsv:
csv.write(re.sub('\t',',',re.sub('(^|[\t])([^\t]*\,[^\t\n]*)', r'\1"\2"', line)))
csv.close()
if __name__ == '__main__':
print('Start converting ...')
convert('table')
print('Converted!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment