Created
May 26, 2019 16:11
-
-
Save jruehlow/68bb40866df2b433a0eac611c926cfe1 to your computer and use it in GitHub Desktop.
Python convert .tsv to .csv file - Including Commas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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