[Tutor] Mapping ID's for corresponding values in different Columns

Fred G bayespokerguy at gmail.com
Sun Jul 8 23:47:47 CEST 2012


Hi--

My current input looks like the following:

FILE1.csv
PERSON_ID    PERSON_NAME
1                     Jen
2                     Mike
3                     Jim
4
5                     Jane
6                     Joe
7                     Jake

FILE2.csv
PERSON_ID   PERSON_NAME
                     Jim
                     Mike
                     Jane
                     Todd
                     Jen

_________
I want to fill into the PERSON_ID column of FILE2.csv the corresponding
ID's associated with those names as identified in FILE1.csv.

At first I imported the csv module and was using the csv.Reader, but then
it seemed simple enough just to write something like:
for line in file2:
     print(line)

giving me the following output:
PERSON_ID, PERSON_NAME
, Jim
, Mike
, Jane
, Todd
, Jen

I think I understand the issue at a conceptual level, but not quite sure
how to fully implement it:
a) I want to build a dictionary to create keys, such that each number in
file1 corresponds to a unique string in column B of file1.
b) then write a for loop like the following:
for "person_name" in file2:
   if "person_name.file2" == "person_name.file1":
       person_id.file2 == person_id.file1
c) write into file2 the changes to person_id's...

But it's pretty difficult for me to get past this stage. Am I on the right
track? And more importantly, how could I learn how to actually implement
this in smaller stages?

Thanks so much.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120708/7fdf5686/attachment.html>


More information about the Tutor mailing list