[英]how do you read data in csv file and print specific ones?
在我的程序中,我要求用戶輸入數字,然后繼續使用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
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]
您要做的是對照用戶輸入limit_amount
測試您正在讀取的row
的值。 為此,您需要知道要將用戶輸入與哪個“列”進行比較,假設您要查找的值位於索引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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.