簡體   English   中英

如何讀取csv文件中的數據並打印特定的文件?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM