Python: Write two lists into two column text file

后端 未结 6 726
我寻月下人不归
我寻月下人不归 2020-12-13 07:23

Say I have two lists: a=[1,2,3] b=[4,5,6] I want to write them into a text file such that I obtain a two column text file:



        
相关标签:
6条回答
  • 2020-12-13 07:45

    You can use numpy.savetxt(), which is a convenient tool from the numpy library. A minimal example would be as follows:

    import numpy as np
    
    xarray = np.array([0,1,2,3,4,5])
    yarray = np.array([0,10,20,30,40,50])
    #here is your data, in two numpy arrays
    
    data = np.array([xarray, yarray])
    data = data.T
    #here you transpose your data, so to have it in two columns
    
    datafile_path = "/your/data/output/directory/datafile.txt"
    with open(datafile_path, 'w+') as datafile_id:
    #here you open the ascii file
    
        np.savetxt(datafile_id, data, fmt=['%d','%d'])
        #here the ascii file is written. 
    

    The '+' in 'w+' in the open() command means 'create if non existent'

    The fmt field in np.savetxt() in the example specifies that the numbers are integers. You can use a different format for each column. E.g. to specify floating point format, with 2 decimal digits, you would use '%.2f'.

    0 讨论(0)
  • 2020-12-13 07:46

    A simple solution is to write columns of fixed-width text:

    a=[1,2,3]
    b=[4,5,6]
    
    col_format = "{:<5}" * 2 + "\n"   # 2 left-justfied columns with 5 character width
    
    with open("foo.csv", 'w') as of:
        for x in zip(a, b):
            of.write(col_format.format(*x))
    

    Then cat foo.csv produces:

    1    4    
    2    5    
    3    6    
    

    The output is both human and machine readable, whereas tabs can generate messy looking output if the precision of the values varies along the column. It also avoids loading the separate csv and numpy libraries, but works with both lists and arrays.

    0 讨论(0)
  • 2020-12-13 07:49

    Try this:

    file = open("list.txt", "w")
    for index in range(len(a)):
        file.write(str(a[index]) + " " + str(b[index]) + "\n")
    file.close()
    
    0 讨论(0)
  • 2020-12-13 07:52

    Simply zip the list, and write them to a csv file with tab as the delimiter:

    >>> a=[1,2,3]
    >>> b=[4,5,6]
    >>> zip(a,b)
    [(1, 4), (2, 5), (3, 6)]
    >>> import csv
    >>> with open('text.csv', 'w') as f:
    ...    writer = csv.writer(f, delimiter='\t')
    ...    writer.writerows(zip(a,b))
    ...
    >>> quit()
    $ cat text.csv
    1       4
    2       5
    3       6
    
    0 讨论(0)
  • 2020-12-13 07:54

    You can write two lists into a text file that contains two columns.

    a=[1,2,3]
    b=[4,5,6] 
    c = [a, b] 
    with open("list1.txt", "w") as file:
        for x in zip(*c):
            file.write("{0}\t{1}\n".format(*x))
    

    Output in the text file:

    1   4
    2   5
    3   6
    
    0 讨论(0)
  • 2020-12-13 08:07

    It exits a straightway to save and stack same vectors length in columns. To do so use the concatenate function, you can then stack 3,4 or N vectors in columns delimitered by a tab.

    np.savetxt('experimental_data_%s_%.1fa_%dp.txt'%(signal,angle,p_range), np.c_[DCS_exp, p_exp], delimiter="\t")
    
    0 讨论(0)
提交回复
热议问题