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

Fred G bayespokerguy at gmail.com
Mon Jul 9 01:50:45 CEST 2012


I thought it made sense to read the two columns in File1 in as a dictionary
(where the key is actually the name, so that we can search on it later),
and the column of interest in File2 as a list.  Finding the common values
then is just:

for item in file2_list:
  for line in file1_dict:
    if item == line:
      print item

Creating a new dictionary and filling it in with the common values gives us
something like:
new_dict = {}
for item in file2_list:
    for line in file1_dict:
      if item == line:
        new_dict[item] = line

I'm not quite sure where to go from here, though. I want:
a) to edit the new_dict such that we get the proper key and value
combination, instead of what it is now which is just the proper values.
b) Once we have the correct dictionary with the proper keys and their
values, how do I then output it into the form (???):

File2.csv
PERSON_ID    PERSON_NAME
key1               Jim
key100           Mike
key3              Jane
key989          Todd
etc...             etc...


On Sun, Jul 8, 2012 at 2:47 PM, Fred G <bayespokerguy at gmail.com> wrote:

> 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/8ee06a88/attachment.html>


More information about the Tutor mailing list