0

I have a list and want to write it into a file without '' , []

d = [['value1', 'value2', 'value3', 'value4', 'LatX', 'LonY', 'HgtZ', 'sats'],
     ['431.84', '1417', '3.63', '10.28', '47.06446389', '15.45043694', '428.08', '6'],
     ['438.25', '1416', '3.63', '10.28', '47.06446583', '15.45044000', '428.00', '6'],
     ['437.64', '1418', '3.63', '9.7', '47.06446583', '15.45044333', '428.03', '6']]

And the file it should look like this:

value1  value2  value3  value4  LatX            LonY         HgtZ    sats        
1415    1417    119     337     47.06446389     15.45043694  428.08  6  
1436    1416    119     337     47.06446583     15.45044000  428.00  6  
1434    1418    119     318     47.06446583     15.45044333  428.03  6

my code (Python 3.7):

with open('your_file.txt', 'w') as f:
    for item in d:
        f.write("%s\n" % str(item))

this function print the lists in d, line by line

2 Answers2

0
with open('yourfile.txt', 'w') as f:
    for sublist in d:
        for s in sublist:
            f.write(str(s) + '\t')
        f.write('\n')

I have tried this one with some sample list and it worked. I am just opening the file and traversing the list of list and putting elements in order.

Note: As many of the great users provided in the comment that this is not a good way if you have to later reconstruct the data again. (maybe space is there as one of the entry).

I was playing with this code after reading the awesome suggestions:

import csv
f = open('testfile.csv', 'w')

with f:
    writer = csv.writer(f)

    for row in d:
        writer.writerow(row)

From documentation:

csvwriter.writerows(rows) Write all elements in rows (an iterable of row objects as described above) to the writer’s file object, formatted according to the current dialect.

reference: 1. Python docs

user27286
  • 486
  • 4
  • 11
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/210514/discussion-on-answer-by-user27286-write-a-file-with-a-list). – Samuel Liew Mar 29 '20 at 05:07
0

Maybe you could use something more pythonic, just for printing do:

def printTable():
    headers = ['value1', 'value2', 'value3', 'value4', 'LatX', 'LonY', 'HgtZ', 'sats']
    table = [['431.84', '1417', '3.63', '10.28', '47.06446389', '15.45043694', '428.08', '6'],
             ['438.25', '1416', '3.63', '10.28', '47.06446583', '15.45044000', '428.00', '6'],
             ['437.64', '1418', '3.63', '9.7', '47.06446583', '15.45044333', '428.03', '6']]
    print ''.join(column.rjust(10) for column in headers)
    for row in table:
        print ''.join(str(column).rjust(10) for column in row)

If you want to write it to a file use a context manager with open('file.txt','w'), but check that your data could fit into a CSV to read it for new runs:

import csv
headers = ['value1', 'value2', 'value3', 'value4', 'LatX', 'LonY', 'HgtZ', 'sats']
table = [['431.84', '1417', '3.63', '10.28', '47.06446389', '15.45043694', '428.08', '6'],
             ['438.25', '1416', '3.63', '10.28', '47.06446583', '15.45044000', '428.00', '6'],
             ['437.64', '1418', '3.63', '9.7', '47.06446583', '15.45044333', '428.03', '6']]
with open('test.csv', 'w', newline ='') as file:
   writer = csv.writer(file, delimiter=',')
   writer.writerow(i for i in headers)
   for j in table:
       writer.writerow(j)
Alan Garrido
  • 604
  • 5
  • 16