【发布时间】: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()
输出只是将 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