在我的程序中,我向用户询问一个数字,然后继续使用DictReader读取csv文件。我想输出csv文件中大于用户数的值....
import csv
limit_amount = raw_input("Please Enter a Limit Amount: ")
with open("transactionsample.csv","rb") as my_file:
reader = csv.DictReader(my_file)
for row in reader:
print row
答案 0 :(得分:1)
import csv
limit_amount = int(raw_input("Please Enter a Limit Amount: ")) #convert limit_amount from string to int
with open("transactionsample.csv") as my_file: #rb is really unnecessary
reader = csv.DictReader(my_file)
for row in reader:
for k in row: #check each cell
if row[k].isdigit() and int(row[k]) > limit_amount: #validation and comparison
print row[k]
答案 1 :(得分:0)
您要做的是根据用户输入row
测试您正在阅读的limit_amount
的值。为此,你需要知道哪一列'您正在比较用户输入,让我们说您要查找的值是索引2
:
import csv
limit_amount = raw_input("Please Enter a Limit Amount: ")
with open("transactionsample.csv","rb") as my_file:
reader = csv.DictReader(my_file)
for row in reader:
if row[2] >= limit_amount: # you might have to change the if condition to match exactly what you want
print row