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

Afonso Duarte aduarte at itqb.unl.pt
Thu May 10 10:20:37 CEST 2012


Dear All,

Thanks Joel and Walter, both solutions worked nicely and produced the desired final objective! 

Short answer to Alan Gauld, far from me to start answering all your points, but I guess I should answer to the most pertinent ones:

>> I'm not sure what you mean. 
>>The answers you got seem to have provided the answers to your questions. 

The answers that solved the problem were only given after I sent the email you answered ... the replies before that (sure you can still see them online) are aimed (I am assuming) at a more advanced level of Python not for a new guy, moreover once those were given and once i started asking how actually that could ever work I got answers arguing that the answer was given...

>>What more were you expecting?

The excellent answers I got late yesterday and that indeed represent the majority of good answers I saw in this mailing list...

>> Which league is that?

The "league of ppl" that actually uses mailing lists in a daily basis (both for asking and answering questions) in different fields, i.e. I am used to mailing list talks and rarely have a problem of miscommunication ... surprisingly for me in this one I got that. Btw just to make it clear again I am a Python beginner (as I said before) working with big files... otherwise I wouldn’t be here asking such questions. 


>>In your case the majority of the answers have come from 
>>experienced programmers giving you sound advice and 
>>probing your requirements to ensure that all your use 
>>cases are covered.

I do agree entirely, that is why I subscribed to this mailing list and not to another! And at the end I did got my answer (but again only after getting emails saying that it was more than explained when it wasn't).

Thanks again for the nice solutions and sorry for the side discussion on how to write/answer to mailing lists 

Cheers

Afonso


-----Original Message-----
From: Joel Goldstick [mailto:joel.goldstick at gmail.com] 
Sent: woensdag 9 mei 2012 20:47
To: Afonso Duarte
Cc: tutor at python.org
Subject: Re: [Tutor] Script to search in string of values from file A in file B

On Wed, May 9, 2012 at 3:40 PM, Joel Goldstick <joel.goldstick at gmail.com> wrote:
> On Wed, May 9, 2012 at 10:00 AM, Afonso Duarte <aduarte at itqb.unl.pt> wrote:
>> Dear All,
>>
>>
>>
>> I’m new to Python and started to use it to search text strings in big
>> (>500Mb) txt files.
>>
>> I have a list on text file (e.g. A.txt) that I want to use as a key 
>> to search another file (e.g. B.txt), organized in the following way:
>>
>>
>>
>> A.txt:
>>
>>
>>
>> Aaa
>>
>> Bbb
>>
>> Ccc
>>
>> Ddd
>>
>> .
>>
>> .
>>
>> .
>>
>>
>>
>> B.txt
>>
>>
>>
>> Bbb
>>
>> 1234
>>
>> Xxx
>>
>> 234
>>
>>
>>
>>
>>
>> I want to use A.txt to search in B.txt and have as output the 
>> original search entry (e.g. Bbb) followed by the line that follows it 
>> in the B.txt (e.g.  Bbb / 1234).
>>
>> I wrote the following script:
>>
>>
>>
>>
>>
>> object = open(B.txt', 'r')
>>
>> lista = open(A.txt', 'r')
>>
>> searches = lista.readlines()
>>
>> for line in object.readlines():
>>      for word in searches:
>>           if word in line:>
>>                print line+'\n'
>>
>>
>>
>
> Don't give up on this group so quickly.  You will get lots of help here.
>
> As to your problem:  Do you know about enumerate?  Learn about it
> here: http://docs.python.org/library/functions.html#enumerate
>
> if you change your code above to:
>   for index, word in enumerate line:
>       print line, word[index+1]
>
> I think you will get what you are looking for

My mistake :  I meant this:
  my_lines = object.readlines()  # note, not a good thing to name something object. Its a class
  for index, line in enumerate(my_lines):
      for word in searches:
           if word in line:
                print line
                print my_lines[index+1]

Sorry for the crazy earlier post

>>
>> But from here I only get the searching entry and not the line 
>> afterwards, I tried to google it but I got lost and didn’t manage to do it.
>>
>> Any ideas ? I guess that this is basic scripting but I just started .
>>
>>
>>
>> Best
>>
>>
>>
>> Afonso
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> --
> Joel Goldstick



--
Joel Goldstick



More information about the Tutor mailing list