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

发布于 2024-10-30 19:52:44 字数 233 浏览 3 评论 0原文

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

{

attribute1 attribute2 attribute3.... attributeN

value"A" value"B" value"C".... value"Z"

/* next line of values*/

}

每个单词都用制表符分隔。

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

I have a text file in this format :

{

attribute1 attribute2 attribute3.... attributeN

value"A" value"B" value"C".... value"Z"

/* next line of values*/

}

Each word is separated by a tab.

How do I convert to CSV format? I tried using Excel but it's giving compatibility issues.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜吻♂芭芘 2024-11-06 19:52:44

使用 Excel 导入数据(数据 > 从文本文件加载),使用制表符作为列分隔符。然后将文件另存为 csv。

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

Import the data with excel (Data > Load from text file), using tab as a column separator. Then save the file as csv.

It cannot have compatibility issues, it's a basic task and i did it quite often in the past.

汹涌人海 2024-11-06 19:52:44

如果您可以使用脚本语言,您可能会给出Python 截图:

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')
    ...

If you can use a scripting language, you might give Python a shot:

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)

Example interpreter session:

>>> 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'

Notes:

Alternative line-terminator example:

with open('yourfile.csv','w') as fou:
    cw = csv.writer(fou,quotechar='',quoting=csv.QUOTE_NONE,lineterminator='\n')
    ...
白首有我共你 2024-11-06 19:52:44

下面是一些可完成此转换的 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

Here's some Excel-VBA code that will do this conversion. Paste this in Excel's visual basic editor (Alt-F11) and run it (after adjusting your filenames, of course).

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
ゝ偶尔ゞ 2024-11-06 19:52:44

只需执行“文件”>另存为...并选择 .csv 文件格式

Just do File > Save as... and choose .csv file format

长亭外,古道边 2024-11-06 19:52:44
  1. 打开 MS Excel
  2. 单击“数据”选项卡
  3. 单击“来自文本”
  4. 选择您的 tsv 文件
  5. 选择分隔
  6. 符 单击“下一步”
  7. 单击“检查”选项卡和逗号
  8. 单击“完成”。
  1. Open MS Excel
  2. Click on Data tab
  3. Click on From text
  4. Choose your tsv file
  5. Choose delimited
  6. Click on Next
  7. Click on Check on tab and comma
  8. Click on Finish.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文