繁体   English   中英

有没有办法为 csv 文件读取 python 3 中某行的特定值?

[英]Is there a way to read specific values of a row in python 3 for csv file?

我想通过输入Customer NRIC号码仅读取Existing Policies最后一列的值。 例如,当我输入S876XXXXF时,output 应该给我现有的政策: Investments and Travel

这是csv文件数据:

Customer NRIC,Customer Name,Date of Birth,Existing Policies
S876XXXXF,John Tan,7-Oct-87,Investments and Travel
T019XXXXA,Alice Lim,14-Apr-01,Savings 
S921XXXXK,Sam Chua,19-Aug-92,Savings and Investment

这是我到目前为止编写的代码并卡住了:

import csv

filename = "database.csv"
nric = input("Enter your NRIC number: ")

file = open(filename,"r")

with open("database.csv") as customer_info:
    csv_pointer = csv.reader(customer_info)
    for each in csv_pointer:
        print(each[-1])

谢谢!

with open('database.csv') as csvfile:
    csv_pointer = csv.reader(csvfile, delimiter=',')
    for row in csv_pointer:
        print(row)

首先,这样做,您将开始了解各个变量存储的内容。 那么我相信你应该可以将代码的rest 只做output 具体的身份证号码。 如果您仍然无法获得它,以下是解决方案。

nric = input("Enter your NRIC number: ")
with open('database.csv', newline='') as csvfile:
    csv_pointer = csv.reader(csvfile, delimiter=',')
    for row in csv_pointer:
        if row[0]==nric:
            print(row[-1])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM