Search code examples
pythonregexsearchloopsfindall

Python Regular Expression loop


I have this code which will look for certain things in a file. The file looks like this:

name;lastname;job;5465465
name2;lastname2;job2;5465465
name3;lastname3;job3;5465465

This is the Python code:

import re
import sys

filehandle = open('somefile.csv', 'r')
text = filehandle.read()
b = re.search("([a-zA-Z]+);([a-z\sA-Z]+);([a-zA-Z]*);([0-9^-]+)\n?",text)
print (b.group(2),b.group(1),b.group(3),b.group(4))

no it will only print:

lastname;name;job;5465465

It supposed to print the lastname first so I did that with groups. Now I need a loop to print all lines like this:

lastname;name;job;5465465
lastname2;name2;job2;5465465
lastname3;name3;job3;5465465l

I tried all kind of loops but it doesn't go through the whole file... how do I need to do this?

it must be done with the re module. I know its easy in the csv module


Solution

  • You need to process the file line by line.

    import re
    import sys
    
    with open('somefile.csv', 'r') as filehandle:
        for text in filehandle:
            b = re.search("([a-zA-Z]+);([a-z\sA-Z]+);([a-zA-Z]*);([0-9^-]+)\n?",text)
            print (b.group(2),b.group(1),b.group(3),b.group(4))
    

    Your file has nicely semi-colon separated values, so it would be easier to just use split or the csv library as has been suggested.