实际上,下面的代码可以成功地将.tsv
文件转换为.csv
文件,但是,当文件很大时(比如超过1GB),它在read
函数中有一个MemoryError
。在
import re
tsv = open('tsv.tsv', 'r')
fileContent = tsv.read()
fileContent = re.sub("\t", ",", fileContent) # convert from tab to comma
csv_file = open("csv.csv", "w")
csv_file.write(fileContent)
csv_file.close()
我知道通过阅读一个大文件,我可以使用以下代码:
^{pr2}$但我不知道如何将这两个代码合并为一个,并正确地将一个大的.tsv文件转换为.csv文件
把你的两个片段直接粘在一起:
对于大文件,使用pandas,而不是纯Python:
相关问题 更多 >
编程相关推荐