【问题标题】:Create a function that uses a list as an argument to extract rows from a CSV创建一个使用列表作为参数从 CSV 中提取行的函数
【发布时间】:2019-10-18 14:31:41
【问题描述】:

我正在尝试将一个列表作为参数传递给一个函数,如果它在提供的列表中包含一个字符串,它将从 csv 中获取一行。我无法在 itemA 上更改索引。它只打印列表的最后一项!

GAS=[
"SUNOCO",
"CUMBERLAND",
"MOBIL"]
gasLength=len(GAS)
print(gasLength)

def parseData(csvToParse = transactionsCSV, itemA="", itemB=""):
#For Loop to append to CSV
    for row in csvToParse:
        if itemA in row[3]:
            csv_personA.writerow([row[0],row[1],row[2],row[3],row[4],row[5]])
            print(row[3])
            print(itemA)
        elif itemB in row[3]:
            csv_personB.writerow([row[0],row[1],row[2],row[3],row[4],row[5]])

#This Was suggested but still only returns the GAS index of 0
for counter, _ in enumerate(range(gasLength)):
    parseData(csvToParse=transactionsCSV, itemA=GAS[counter], itemB="")



for _ in range(gasLength):
    x = gasLength-1
    parseData(csvToParse=transactionsCSV, itemA=GAS[x], itemB="")


# My first attempt is below!!!
#Get gas purchases

def parseGasStations():
    x = 0
    itemsToCheck = row_count*gasLength
    print(itemsToCheck)
     #while x is less than total of items in the main csv times the number of items in the gas array.
    while x < itemsToCheck:
        a = 0
        y = 0
        #While a is less than the total number of rows in the main 
        while a < row_count: 
            print(GAS[y])
        for _ in range(gasLength):
            parseData(csvToParse=transactionsCSV, itemA=GAS[gasLength-1], itemB="")
            if y != gasLength-1: 
                y += 1
            elif y == gasLength-1:
                y = 0
            a += 1
        x += 1
parseGasStations()

csv output

输出只是将 MOBIL 站附加到 CSV,而不是像我想象的那样对列表进行索引。

【问题讨论】:

  • 你能解释一下你所期望的实际输出是什么以及输入的一点点(如果你能举个例子)
  • 我刚刚在上面添加了更多代码...我想从包含项目的 csv 中提取 GAS 中的项目。由于某种原因,我无法遍历 GAS,无论我尝试什么,我都只能返回初始索引值。
  • 你能展示一个示例行的样子吗?
  • POS 借记卡 5/17/19 检查 SUNOCO ANYWHERE IL -20.2。从运行计数器返回,_ in enumerate(range(gasLength)): parseData(csvToParse=transactionsCSV, itemA=GAS[counter], itemB="")

标签: python list function parameters


【解决方案1】:

感谢 Fluxens,我能够解决这个问题! 这是一个将列表作为参数并索引所有项目的函数!


GAS=(
"SUNOCO",
"CUMBERLAND",
"MOBIL",
"BESTWAY",
"AMORE FUEL")
gasLength=len(GAS)

def parseData(csvToParse="", catagory=(), catagorySize=""):
    #For loop to check each row in master csv
    for row in csvToParse:
        #For loop to index through catagory items to look for in each row
        for counter, _ in enumerate(range(catagorySize)):
            if catagory[counter] in row[3]:
                csv_mark.writerow([row[0],row[1],row[2],row[3],row[4],row[5]])
                print(row[3])
                print(catagory)

parseData(csvToParse = transactionsCSV, catagory=GAS, catagorySize=gasLength)

【讨论】:

    【解决方案2】:

    因此,如果您想在迭代中使用数字迭代计数器,您可以执行以下操作。

    for counter, _ in enumerate(range(gasLength)):
        parseData(csvToParse=transactionsCSV, itemA=GAS[counter], itemB="")
    

    枚举返回一个包含计数器和元素本身的元组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多