用列表写文件

问题描述 投票:0回答:1

我有一个列表,想将其写入不带'',[]]的文件中>

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

并且在文件中应该看起来像这样:

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

我的代码(Python 3.7):

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

此函数逐行打印d中的列表

我有一个列表,想将其写入不带''的文件中,[] d = [['value1','value2','value3','value4','LatX','LonY','HgtZ ','sats'],['431.84','1417','3.63','10 .28','47 .06446389','...

python list file writefile
1个回答
0
投票
with open('yourfile.txt', 'w') as f:
    for list in d:
        for s in list :
            f.write(str(s) + ' ')
        f.write('\n')
© www.soinside.com 2019 - 2024. All rights reserved.