[Tutor] Script to search in string of values from file A in file B
wprins at gmail.com
Wed May 9 22:18:30 CEST 2012
I see you've had some responses yet -- I've not read them all, and am just
posting the following suggestion you might want to look at:
# read lines with "keys" into a list
# read all data records into another list
# Now use a list comprehension to return the required entries, the i+1th
entries for all i indexes in the records
# list that corresponds to a key in the keys list:
selected_values = [(records[i], records[i+1]) for i, row in
enumerate(records) if row in selected_keys]
# The above returns both the key and the value, in a tuple, if you just
want the value rows only then the above becomes:
#selected_values = [records[i+1] for i, row in enumerate(records) if row in
# Finally print the result.
You'll note I read both files into memory, even though you say your files
are largish. I don't consider 500MB to be very large in this day and age
of 4+GB PC's, which is why I've basically ignored the "large" issue. If
this is not true in your case then you'll have to post back.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor