user2048212
user2048212

Reputation: 1

searching a csv and printing a line

Trying to create a train booking system. Having trouble searching my csv and printing that certain line.

The user already has there id number,and the csv is is set out like

This is what I have so far:

Upvotes: 0

Views: 99

Answers (2)

ben_frankly
ben_frankly

Reputation: 9950

Try using the built-in CSV module. It will make things easier to manage as your requirements change.

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'

Upvotes: 1

isedev
isedev

Reputation: 19641

You are matching the entire line against the ID. You need to split out the first field and check that:

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"

Upvotes: 2

Related Questions