循环一次添加一行到列表

时间:2018-10-22 11:39:12

标签: python list csv

我有下面的代码来创建ItemID和其他功能的列表,这些功能可以从csv创建其他列表。我尝试编写一个循环循环的函数,一次向另一列表添加一行(为每个ItemID添加一个新的价格行),而不是每次循环都创建完整列表。

def Main(filename1, filename2):
    with open(filename1, "r") as csv1, open(filename2, "r") as csv2:  
        csvReader1 = csv.DictReader(csv1)
        csvReader2 = csv.DictReader(csv2)

        ItemIDList = []
        for row1, row2 in zip(csvReader1, csvReader2):  
          ItemIDList.append((row2["ItemId"]))
    return ItemIDList

def buildObject(ItemIDList):

    for row in ItemIDList: getPrice(filename1, filename2)
    for row in ItemIDList: NameList = getName(filename1, filename2)

def getPrice(filename1, filename2):
    with open(filename1, "r") as csv1, open(filename2, "r") as csv2:  
        csvReader1 = csv.DictReader(csv1)
        csvReader2 = csv.DictReader(csv2)

        priceList = []
        for row1, row2 in zip(csvReader1, csvReader2):  
          csvPVList.append((row2["Price"]))
        return priceList

Main会构建ID列表,getPrice是我试图重新编写的函数,每次只能在buildObject中的csv每个循环中一次添加一行。

1 个答案:

答案 0 :(得分:0)

这是一个设计问题。从磁盘读取文件是一项昂贵的操作,应该只执行一次。并且将两个csv文件加载到内存中以对其进行压缩并不有效。所以你应该:

  • 具有单个提取器方法/功能,该方法一次可读取一行两个文件,并向列表(或元组/命名元组/对象的列表)提供下一次使用的所有数据
  • 让此提取器返回这些列表

如果使用单个列表,则代码可以是(高度简化,没有错误处理):

def extract_data(filename1, filename2):
    with open(filename1, "r") as csv1, open(filename2, "r") as csv2:  
        csvReader1 = csv.DictReader(csv1)
        csvReader2 = csv.DictReader(csv2)

        ItemIDList = []
        priceList = []
        for row1 in csvReader1:
            row2 = next(csvReader2)
            ItemIDList.append(row2["ItemId"])
            priceList.append(row2["Price"])
            ...                                    # eventually extract other fields
    return ItemIDList, priceList

或者如果使用namedtuples:

from collections import namedtuple
Record = namedtuple("ItemId", "Price")
...
            recordList = []
            for row1 in csvReader1:
                row2 = next(csvReader2)
                recordList.append(Record(row2["ItemId"],row2["Price"]))
        return recordList