多个列表输入到文件

时间:2017-12-04 04:03:19

标签: python

我有4个长度相等的列表。 像:

List1: x1,x2,x3,x4...
List2: y1,y2,y3,y4...
List3: z1,z2,z3,z4,...
list4: a1,a2,a3,a4,....

我想阅读它们并将文件写为:

X1,Y1,Z1,A1 ....

为此:

with open('do.txt','a+') as writefile:
   for i in iter(List1):
      writefile.write(i)

我们怎样才能同时写入文件中的所有文件。

3 个答案:

答案 0 :(得分:4)

zip应解决您的问题

with open('do.txt','a+') as writefile:
    for a,b,c,d in zip(list1,list2,list3,list4):
        writefile.write("{},{},{},{}\n".format(a,b,c,d))

答案 1 :(得分:0)

media stateszip

with open('do.txt','a+') as writefile:  
    writefile.write('\n'.join(','.join(t for t in zip(list1,list2,list3,list4))))

答案 2 :(得分:0)

只是为了好玩这是一线解决方案:)

with open('writing_file.txt','a+') as file_1:
    for a,b,c,d in zip(list1,list2,list3,list4):
        file_1.write("{},{},{},{}\n".format(a,b,c,d))

以上列表理解与:

相同
JSONArray