1 csv文件:逗号分隔符文件,可以使用excel打开。逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值。
2 tsv文件:制表符Tab分隔文件,可二以使用文本文档打开。
import pandas as pd
data = pd.read_csv(r'E:\XX.tsv', sep='\t')
data.to_csv(r'E:\XX.csv')
另外一种正则表达式的方法
# Python program to convert .tsv file to .csv file
# importing re library
import re
# reading given tsv file
with open("Olympic.tsv", 'r') as myfile:
with open("Olympic.csv", 'w') as csv_file:
for line in myfile:
# Replace every tab with comma
fileContent = re.sub("\t", ",", line)
# Writing into csv file
csv_file.write(fileContent)
# output
print("Successfully made csv file")
import pandas as pd
data = pd.read_csv(r"E:\XX.csv", sep=',', encoding='utf-8')
data.to_csv(r"E:\XX.tsv", index=False, sep='\t', encoding='utf-8')