我有一个这样格式的文本文件。
{
attribute1 attribute2 attribute3.... attributeN
value"A" value"B" value"C".... value"Z"
/* next line of values*/
}
每一个字都用一个制表符隔开。
我如何将其转换为CSV格式?我尝试使用Excel,但它给出了兼容性问题。
用excel导入数据(Data > Load from text file),使用tab作为列分隔符。然后将文件保存为csv。
它不可能有兼容性问题,这是一个基本的任务,我过去也经常这样做。
import csv
# read tab-delimited file
with open('yourfile.tsv','r') 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','w') as fou:
cw = csv.writer(fou, quotechar='', quoting=csv.QUOTE_NONE)
cw.writerows(filecontents)
解释会议的例子。
>>> import csv
>>> with open('yourfile.tsv','r') as fin:
... cr = csv.reader(fin, delimiter='\t')
... filecontents = [line for line in cr]
...
>>> with open('yourfile.csv','w') as fou:
... cw = csv.writer(fou, quotechar='', quoting=csv.QUOTE_NONE)
... cw.writerows(filecontents)
...
>>> with open('yourfile.csv','r') 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'
注意:
另一种行结束符的例子。
with open('yourfile.csv','w') as fou:
cw = csv.writer(fou,quotechar='',quoting=csv.QUOTE_NONE,lineterminator='\n')
...
这里有一些Excel-VBA代码可以进行这种转换。将其粘贴到Excel的visual basic编辑器中(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