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

Walter Prins wprins at gmail.com
Mon Jul 9 02:09:02 CEST 2012


On 9 July 2012 00:50, Fred G <bayespokerguy at gmail.com> wrote:
> 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

yes...

> 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

No.  Once you have a dictionary from which you can look up id's from
names, you want to simply read through the lines in file 2, and for
each line, you want to pick up the name from that line and look up the
corresponding id using the dict.  Then, having looked up the number,
you want to then write out a line to a new version of File2 that will
replace File2 once done.  That's more or less it.  Exactly how you
handle the old/new File2 issue is up to you.  Either write a new file
and rename the files once done (safer since you can keep the old file
as a backup), or read the entire file into memory and then process and
write the whole thing back out to the same file (less safe -- what
happens if something goes wrong while you're writing out the data to
the file?)

Walter


More information about the Tutor mailing list