如何将制表符分隔文件转换为CSV格式?

我有这种格式的文本文件:

{ attribute1 attribute2 attribute3.... attributeN value"A" value"B" value"C".... value"Z" /* next line of values*/ } 

每个单词由一个制表符分隔。

我如何转换为CSV格式? 我尝试使用Excel,但它给兼容性问题。

使用Excel导入数据(数据>从文本文件加载),使用选项卡作为列分隔符。 然后将该文件保存为csv。

它不能兼容性问题,这是一个基本的任务,我过去经常这样做。

如果你可以使用脚本语言 ,你可以给Python一个镜头:

 import csv # read tab-delimited file with open('yourfile.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('yourfile.csv','wb') as fou: cw = csv.writer(fou, quotechar='', quoting=csv.QUOTE_NONE) cw.writerows(filecontents) 

示例解释器会话:

 >>> import csv >>> with open('yourfile.tsv','rb') as fin: ... cr = csv.reader(fin, delimiter='\t') ... filecontents = [line for line in cr] ... >>> with open('yourfile.csv','wb') as fou: ... cw = csv.writer(fou, quotechar='', quoting=csv.QUOTE_NONE) ... cw.writerows(filecontents) ... >>> with open('yourfile.csv','rb') as see_how_it_turned_out: ... for line in see_how_it_turned_out: ... line ... 'attribute1,attribute2,attribute3,attributeN\r\n' 'value"A",value"B",value"C",value"Z"\r\n' 

笔记:

  • 默认的字段分隔符是,

  • csv.writer的默认行结束 csv.writer\r\n ,但是如果您需要这样做,您可以指定另一个关键字参数AKA kwarg

替代行终止符的例子:

 with open('yourfile.csv','wb') as fou: cw = csv.writer(fou,quotechar='',quoting=csv.QUOTE_NONE,lineterminator='\n') ... 

这里有一些Excel-VBA代码将执行此转换。 将其粘贴到Excel的可视化基本编辑器( Alt-F11 )中并运行(当然在调整文件名后)。

 Sub TabToCsv() Const ForReading = 1, ForWriting = 2 Dim fso, MyTabFile, MyCsvFile, FileName Dim strFileContent as String Set fso = CreateObject("Scripting.FileSystemObject") ' Open the file for input. Set MyTabFile = fso.OpenTextFile("c:\testfile.dat", ForReading) ' Read the entire file and close. strFileContent = MyTabFile.ReadAll MyTabFile.Close ' Replace tabs with commas. strFileContent = Replace(expression:=strFileContent, _ Find:=vbTab, Replace:=",") ' Can use Chr(9) instead of vbTab. ' Open a new file for output, write everything, and close. Set MyCsvFile = fso.OpenTextFile("c:\testfile.csv", ForWriting, True) MyCsvFile.Write strFileContent MyCsvFile.Close End Sub