簡體   English   中英

搜索CSV並打印行

[英]searching a csv and printing a line

嘗試創建火車預訂系統。 無法搜索我的csv並打印該特定行。

用戶已經有ID號,並且csv設置為

這是我到目前為止的內容:

您正在將整個行與ID匹配。 您需要拆分第一個字段並檢查:

def buySeat():
    id = raw_input("please enter your ID")

for line in open("customers.csv"):
    if line.split(',')[0] == id:
        print line
    else:
        print "sorry cant find you"

嘗試使用內置的CSV模塊。 隨着需求的變化,這將使事情更易於管理。

import csv

id = raw_input("please enter your ID")

ID_INDEX = 0

with open('customers.csv', 'rb') as csvfile:
    csvReader = csv.reader(csvfile)
    for row in csvReader:
        # Ignore the column names on the first line.
        if row[ID_INDEX] != 'counter':
            if row[ID_INDEX] == id:
                print ' '.join(row)
            else:
                print 'sorry cant find you'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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