如何在csv文件中读取数据并打印特定的数据? [英] how do you read data in csv file and print specific ones?

查看:908
本文介绍了如何在csv文件中读取数据并打印特定的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序,我问用户一个数字,然后继续使用DictReader读取csv文件。我想输出的csv文件中的值大于用户数.... ....

in my program i ask the user for a number and then proceed to read a csv file using DictReader. I want to output the values in the csv file that are larger then the users number....

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]

这篇关于如何在csv文件中读取数据并打印特定的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆