简体   繁体   English

如何读取csv文件中的数据并打印特定的文件?

[英]how do you read data in csv file and print specific ones?

in my program i ask the user for a number and then proceed to read a csv file using DictReader. 在我的程序中,我要求用户输入数字,然后继续使用DictReader读取csv文件。 I want to output the values in the csv file that are larger then the users number.... 我想在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]

What you want to do is to test the value from you row you are reading against the user input limit_amount . 您要做的是对照用户输入limit_amount测试您正在读取的row的值。 For that you need to know which 'column' you're comparing the user input against, let's say the value you're looking for is at index 2 : 为此,您需要知道要将用户输入与哪个“列”进行比较,假设您要查找的值位于索引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