Python Forum

Full Version: searching contents of files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
my question here

i have two files a and b.I want to search contents of file a in file b.and the out put to other file which contains only one word after line number.the two files are below:

File a:
5000cca025884d5
5000cca025a1ee6

File b:

0. c0t5000CCA025A1EE6Cd0 <preSUN30G-A2B0-279.40GB>
         /scsi_vhci/disk@g5000cca025a1ee6c
      1. c0t5000CCA025A28FECd0 <preSUN30G-A2B0-279.40GB>
      i/disk@g5000cca025a28fec
      2. c0t5000CCA0258BA1DCd0 <HsdfdsSUN30G-A2B0 cyl 46873 alt 2 hd 20 sec 625>
        i/disk@g5000cca0258ba1dc
      3. c0t5000CCA025884D5Cd0 <UN300G cyl 46873 alt 2 hd 20 sec 625>  solaris
       i/disk@g5000cca025884d5c
      4. c0t5000CCA02592705Cd0 <UN300G cyl 46873 alt 2 hd 20 sec 625>  solaris
       i/disk@g5000cca02592705c
(Jul-19-2017, 08:48 PM)Ahmedrhle Wrote: [ -> ]and the out put to other file which contains only one word after line number
You what?

So there's a third file, for output? Or you're overwriting the second file with the output?
Given the two sample input files, what would your expected output be?
What have you tried so far?
Thank a lot it will be the new file.

Sample input files are above in the thread

My expected out put will be the word after line number
(Jul-19-2017, 08:55 PM)Ahmedrhle Wrote: [ -> ]Sample input files are above in the thread
What about sample output?
And the code you've tried so far?
I tried below code.but of no use.

>>> master_file=open("/var/tmp/out1","r")
>>> search_results=open("/var/tmp/out2","w")
>>> for line in master_file:
... if set(line.split()[:-1]) & keywords:
... search_results.write(line)
Obviously it's not useful, you're trying to do a set AND against an undefined variable.

>>> with open("a.txt") as master_file:
...   with open("b.txt") as search_results:
...     for line in master_file:
...       if set(line.split()[:-1]) & keywords:
...         # doesn't matter
...         pass
...
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
NameError: name 'keywords' is not defined
Please can you give the code for getting the output.
(Jul-19-2017, 09:37 PM)Ahmedrhle Wrote: [ -> ]Please can you give the code for getting the output.
Nah. I'm here to help people learn python, not to do people's jobs for them lol
Any hints to process with my code.
with open("a.txt") as master_file:
    keywords = {line.strip() for line in master_file} # this is a set comprehension
    # https://docs.python.org/3/library/stdtypes.html#types-set
    # https://docs.python.org/3/tutorial/datastructures.html#sets
    # this gives you an unique set of the lines in master_file
    # line.strip() removes newlines and whitespace
    # all happens inside memory, if the file is bigger than your memory, this doesn't work

# here you can work with keywords.
# just iterate over the second file, split the line, select the right field and look if this field
# is in keyweords, if True, then do something with the information
# like writing it to a third file
Pages: 1 2