我正在编写代码,使用列表推导(row[0]
和row[1]
分别填充CSV文件(transaction_list
和revenue_list
)中的两列在Python 3.6.3中。
如果我运行代码,我可以让脚本正确填充revenue_list
,但transaction_list
将返回一个空列表。
import os, csv
csvpath = os.path.join('budget_data_1.csv')
with open(csvpath) as f:
readcsv = csv.reader(f, delimiter=',')
next(readcsv)
revenue_list = [int(row[1]) for row in readcsv]
print(revenue_list)
transaction_list = [row[0] for row in readcsv]
print(transaction_list)
另一方面,如果我运行代码(注释掉revenue_list = [int(row[1]) for row in readcsv]
),我可以让transaction_list
返回正确的列表。
import os, csv
csvpath = os.path.join('budget_data_1.csv')
with open(csvpath) as f:
readcsv = csv.reader(f, delimiter=',')
next(readcsv)
#revenue_list = [int(row[1]) for row in readcsv]
#print(revenue_list)
transaction_list = [row[0] for row in readcsv]
print(transaction_list)
问题:当我尝试填充transaction_list
和 transaction_list
时,为什么脚本会将revenue_list
作为空列表返回写?