How to convert a tab separated file to CSV format?

前端 未结 3 1692
梦如初夏
梦如初夏 2021-01-01 13:01

I have a text file in this format :

{

attribute1 attribute2 attribute3.... attributeN

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

/* next line of val         


        
相关标签:
3条回答
  • 2021-01-01 13:31

    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:

    • The default field delimiter is ,.

    • The default line terminator for csv.writer is \r\n, but should you have a need to do so you may specify an alternative as a keyword argument AKA kwarg.

    Alternative line-terminator example:

    with open('yourfile.csv','w') as fou:
        cw = csv.writer(fou,quotechar='',quoting=csv.QUOTE_NONE,lineterminator='\n')
        ...
    
    0 讨论(0)
  • 2021-01-01 13:44

    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.

    0 讨论(0)
  • 2021-01-01 13:44

    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
    
    0 讨论(0)
提交回复
热议问题