Search code examples
pythonregexsearchreadlinegetline

Reading files line-by-line and searching for patterns in python


I am reading a file line-by-line and store the information, but some lines are slightly different and needs to be sorted out.

I have a text file with 502 lines and it is something like

VV item1 value1 item2 value2 itemA valA
VV item1 value1 item2 value2 itemB valB
...

I am storing these values by reading the text line-by-line, but there are differences between the lines. Instead of itemA, I have itemB sometimes. I first tried

ifn = open(afile,'r')
while 1:                                                                                                                                                                                                                                                                       
    l = ifn.readline() 
    print(int(l.split(' ')[1])) #prints value1 as expected
    if re.search('itemB', l):
            print(int(l.split(' ')[6]))

This last print statement doesn't print anything although it supposes to and ignores it totally.

Then I also tried

ifn = open(afile,'r')
for i,l in enumerate(ifn):                                                                                                                                                                                                                                                           
        print( linecache.getline(ifn.name,i).split(' ')[1]) )

but it prints a blank line before implementing the re.search statement.

I'd appreciate any help on either one of the approaches.

Thanks you.


Solution

  • I suspect that its your while loop + readline() that's causing the problem. This code should work if you use a context manager and a for loop:

    with open('test.txt','r') as file:                                                                                                                                                                                                                                             
        for line in file:
            if 'itemB' in line:
                print(line.split(' ')[5])
    

    Also, it looks like you're trying to access the 6th item in the line - which means you need to use index 5