我有一个这种格式的文本文件:
{
attribute1 attribute2 attribute3.... attributeN
value"A" value"B" value"C".... value"Z"
/* next line of values*/
}
每个单词由制表符分隔。
如何转换为CSV格式?我尝试使用Excel,但它提供了兼容性问题。
答案 0 :(得分:25)
使用tab作为列分隔符,使用excel(数据>从文本文件加载)导入数据。然后将文件另存为csv。
它没有兼容性问题,这是一项基本任务,我过去经常这样做。
答案 1 :(得分:12)
如果您可以使用scripting language,则可以给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
的{{3}}为\r\n
,但如果您需要这样做,您可以指定替代方案作为关键字参数AKA kwarg
替代行终止符示例:
with open('yourfile.csv','wb') as fou:
cw = csv.writer(fou,quotechar='',quoting=csv.QUOTE_NONE,lineterminator='\n')
...
答案 2 :(得分:0)
这是一些将进行此转换的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
答案 3 :(得分:-2)