Python双重列表到文本文件

时间:2019-05-20 17:30:55

标签: python file

所以我有一个这种格式的列表:

[(532, 'working'), (417, 'texting')]

我想以以下格式将其写入文本文件中:

532 working

417 texting

1 个答案:

答案 0 :(得分:0)

以下实现按预期工作:

input = [(532, 'working'), (417, 'texting')]

with open("output.txt", "w") as opened_file:
    for element in input:
        opened_file.write("%s %s\n" % (element[0], element[1]))

输出:

>>>cat output.txt 
532 working
417 texting