开发者

Help with finding item in csv in python

I'm very novice in python. I have an unchanging cs开发者_如何转开发v, here's an example (I just printed it by row in the python console)

['george', 'williams', '277389', 'susan thompson', '2042228888']
['john', 'smith', '833999', 'george smith', '2041118833']
['michael', 'jackson', '281038', 'ronald jackson', '2041128493']

these are the field titles

['firstname', 'lastname', 'idnumber', 'emergency contact', 'emerg contact ph']

I need to be able to type in the id number, which initiates a search through the csv, and outputs the individual's firstname, lastname, emergency contact, phone number. Any thoughts? I really need to know where to start, i.e., should I read the contents of the csv into a dict


I would personally go with a dictionary:

records = [
    ['george', 'williams', '277389', 'susan thompson', '2042228888'],
    ['john', 'smith', '833999', 'george smith', '2041118833'],
    ['michael', 'jackson', '281038', 'ronald jackson', '2041128493'],
    ]

from operator import itemgetter
recordsbyid = dict(zip(map(itemgetter(2),records),records))

then you can do

>>> recordsbyid['277389']
['george', 'williams', '277389', 'susan thompson', '2042228888']

itemgetter selects the second element (id), map applies to every record, and zip concatenates the ids with their records into a list of tuples consisting of (id,record). dict turns this into a dictionary.


I would convert this csv to sqlite and use a query:

SELECT * FROM data WHERE idnumber = %s

I like working with the data organized in DB and it might bring you more advantages in the future (more sophisticated queries).

To convert the cvs to sqlite and test queries use SQLite Manager addon for Firefox.


If you need to scan the file and find the required row just once, you don't need to convert all data into a dictionary - just read the lines one by one until you find your row:

import csv

def find_row_by_id(filename, key_column, id):
    with f = open(filename, 'rb'):
        my_reader = csv.reader(f)
        for row in my_reader:
            if row[key_column] == id:
                return row
    raise Error("Could not find row")

print find_by_row('eggs.csv', 2, my_id) # my_id should by a string

If your file is small and you need to do multiple searches only by id, convert it to a dictionary as suggested by other answers.

On the other hand, if your file is very (very) big and you need to make fast look ups and/or many look ups, read your csv file into a key-value database first: Reliable and efficient key--value database for Linux?


Use raw_input to get the wanted id.

When you have it use an if statement to see if the id matches:

Where line is ['george', 'williams', '277389', 'susan thompson', '2042228888']

if line[2] == id:
    for x in line:
        if not x == line[2]:
            print x

Hope it helps!


Let's make each line into a dict first, using the field titles as keys and the field data as values. We want a dict where each key-value pair comes from a pair that we grab by "zipping" those two lists together: dict(zip(field_titles, row)). We can use a list comprehension to make a list of these dicts from the list of input rows: [dict(zip(field_titles, row)) for row in data].

We want to be able to look these up by id_number, so we'll make a containing dict where the key is the idnumber and the value is the row dict. We can get the idnumber by looking it up in the row_dict: dict((row_dict['id_number'], row_dict) for row_dict in data).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜