简体   繁体   English

Python 3.6。 如何在for循环中遍历多个列表,然后使用DictWriter()将它们写入CSV文件

[英]Python 3.6. How to iterate over multiple list in a for loop and write them to a CSV File using DictWriter()

I am trying to iterate over multiple lists and save them as CSV files to open in a spreadsheet, with the first list adding it's values to columns 1, 2, and 3, and the second list adding it's values to columns 4, 5, 6. I am instead getting line 1 of list one the len(list 2) amount of times and then the second line of list 2 the same and so forth. 我正在尝试遍历多个列表并将其另存为CSV文件以在电子表格中打开,第一个列表将其值添加到第1、2和3列,第二个列表将其值添加到第4、5、6列我改为让列表1的第1行达到len(list 2)次,然后使列表2的第二行相同,依此类推。 List 2 prints out as normal and then prints again once the second line of list 1 finally prints. 列表2正常打印,然后在列表1的第二行最终打印完后再次打印。 Hopefully I explained this well enough. 希望我对此做了足够的解释。

ixl_file = open('ixalan.txt', 'r')
ixl_card = []
ixl_price = []
ixl_quantity = []
for line in ixl_file:
    info = line.split("|")
    ixl_card.append(info[0])
    ixl_price.append(info[1])
    ixl_quantity.append(info[2])
ixl_file.close()

kal_file = open('kaladesh.txt', 'r')
kal_card = []
kal_price = []
kal_quantity = []
for line in kal_file:
    info = line.split("|")
    kal_card.append(info[0])
    kal_price.append(info[1])
    kal_quantity.append(info[2])
kal_file.close()

with open('card_kingdom.csv', 'w', newline='') as ixl:
        fieldnames = ['Col 1', 'Col 2', 'Col 3', 'Col 4', 'Col 5', 'Col 6']
    writer = csv.DictWriter(ixl, fieldnames=fieldnames)
    writer.writeheader()

"""Below is where I am having trouble add these two list in the desired format""" “”“下面是我在以所需格式添加这两个列表时遇到的问题”“”

    for a,b in itertools.product(range(len(ixl_card)), range(len(kal_card))):
        writer.writerow({'Col 1': ixl_card[a],
                         'Col 2': ixl_price[a],
                         'Col 3': ixl_quantity[a],
                         'Col 4': kal_card[b],
                         'Col 5': kal_price[b],
                         'Col 6': kal_quantity[b]
                       })

Your help would be much appreciated! 您的帮助将不胜感激! Let me know if you need the rest of the code. 让我知道您是否需要其余的代码。 Thanks!! 谢谢!!

This could be done using just a normal CSV writer as all of your columns appear to be in the same order: 只需使用普通的CSV编写器即可完成此操作,因为所有列的显示顺序都相同:

import csv    

with open('ixalan.txt', newline='') as f_ixalan:
    ixalan = list(csv.reader(f_ixalan, delimiter='|'))

with open('kaladesh.txt', newline='') as f_kaladesh:
    kaladesh = list(csv.reader(f_kaladesh, delimiter='|'))

with open('card_kingdom.csv', 'w', newline='') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(['Col 1', 'Col 2', 'Col 3', 'Col 4', 'Col 5', 'Col 6'])

    for p1, p2 in zip(ixalan, kaladesh):
        csv_output.writerow(p1 + p2)

Also your two .txt files can be read using a csv.reader() by specifying a | 您也可以使用csv.reader()通过指定|来读取您的两个.txt文件| for the delimiter. 用于分隔符。 Using zip() takes one row from each of your two input lists, these can then be combined and written to your output CSV. 使用zip()从两个输入列表的每一行中提取一行,然后可以将它们组合并写入输出CSV。

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

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