简体   繁体   English

打印到文件.CSV PYTHON时枚举行

[英]Enumerate lines while printing to to file .CSV PYTHON

I have a simple list of lists. 我有一个简单的清单清单。 4 lists each with 3 values in each 4个列出,每个列出3个值

with open('test2.csv', 'w',) as f2:
 f = csv.writer(f2, delimiter= '\t')
 header = ["SENSOR", "h1", "h2", "h3"]
 f.writerow(header)
 for idx, lists in final_list:

        f.writerow(lists)
        print "IDX:",idx, "LISTS:", lists

errors occur with "for idx, lists in final_list:" and it says: “对于idx,final_list中的列表:”发生错误,并显示:

ValueError: too many values to unpack

You were not far :-) you just need : 你不远:-)你只需要:

for idx, lists in enumerate(final_list):
    f.writerow(lists)
    print "IDX:",idx, "LISTS:", lists

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM