[Tutor] Script to search in string of values from file A in file B

Walter Prins wprins at gmail.com
Wed May 9 22:18:30 CEST 2012


Hi Alfonso,

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
selected_keys=open('A.txt', 'r').readlines()
# read all data records into another list
records=open('B.txt', 'r').readlines()

# 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
selected_keys]

# Finally print the result.
print selected_values

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.

Good luck,

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120509/2ba6cc7c/attachment.html>


More information about the Tutor mailing list