Convert a space delimited file to comma separated values file in python

前端 未结 8 984
陌清茗
陌清茗 2021-01-04 06:11

I am very new to Python. I know that this has already been asked, and I apologise, but the difference in this new situation is that spaces between strings are not equal. I

相关标签:
8条回答
  • 2021-01-04 06:33

    replace your first bit with this. it's not super pretty but it will give you a csv format.

    with open('coord') as infile, open('coordv', 'w') as outfile:
        for line in infile:
            outfile.write(" ".join(line.split()).replace(' ', ','))
            outfile.write(",") # trailing comma shouldn't matter
    

    if you want the outfile to have everything on different lines you could add outfile.write("\n") at the end of the for loop, but i dont think your code that follows this will work with it like that.

    0 讨论(0)
  • 2021-01-04 06:41

    You can use python pandas, I have written your data to data.csv:

    import pandas as pd
    >>> df = pd.read_csv('data.csv',sep='\s+',header=None)
    >>> df
         0  1  2         3         4  5
    0    1  C  6  0.000000  1.342650  0
    1    2  C  6  0.000000 -1.342650  0
    2    3  C  6  2.325539  2.685301  0
    3    4  C  6  2.325539 -2.685301  0
    4    5  C  6  4.651077  1.342650  0
    5    6  C  6  4.651077 -1.342650  0
    ...
    

    The great thing about this is to access the underlying numpy array you can use df.values:

    >>> type(df.values)
    <type 'numpy.ndarray'>
    

    To save the data frame with comma delimiters:

    >>> df.to_csv('data_out.csv',header=None)
    

    Pandas is a great library for managing large amounts of data, as a bonus it works well with numpy. There is also a very good chance that this will be much faster then using the csv module.

    0 讨论(0)
  • 2021-01-04 06:44
    >>> a = 'cah  1  C       6.00    0.000000000    1.342650315    0.000000000'
    =>  a = 'cah  1  C       6.00    0.000000000    1.342650315    0.000000000'
    
    >>> a.split()
    =>  ['cah', '1', 'C', '6.00', '0.000000000', '1.342650315', '0.000000000']
    
    >>> ','.join(a.split())
    =>  'cah,1,C,6.00,0.000000000,1.342650315,0.000000000'
    
    >>> ['"' + x + '"' for x in a.split()]
    =>  ['"cah"', '"1"', '"C"', '"6.00"', '"0.000000000"', '"1.342650315"', '"0.000000000"']
    
    >>> ','.join(['"' + x + '"' for x in a.split()]
    =>  '"cah","1","C","6.00","0.000000000","1.342650315","0.000000000"'
    
    0 讨论(0)
  • 2021-01-04 06:50

    You can use csv:

    import csv
    
    with open(ur_infile) as fin, open(ur_outfile, 'w') as fout:
        o=csv.writer(fout)
        for line in fin:
            o.writerow(line.split())
    
    0 讨论(0)
  • 2021-01-04 06:53

    The csv module is good, or here's a way to do it without:

    #!/usr/local/cpython-3.3/bin/python
    
    with open('input-file.csv', 'r') as infile, open('output.csv', 'w') as outfile:
        for line in infile:
            fields = line.split()
            outfile.write('{}\n'.format(','.join(fields)))
    
    0 讨论(0)
  • 2021-01-04 06:54

    for converting "space" to ","

    only fill the filename to what you want

    with open('filename') as infile, open('output', 'w') as outfile:
        outfile.write(infile.read().replace(" ", ","))
    

    for converting "," to "Space"

    with open('filename') as infile, open('output', 'w') as outfile: outfile.write(infile.read().replace(",", " "))

    0 讨论(0)
提交回复
热议问题