princess of persia
princess of persia

Reputation: 2232

Python file regex matching

I am trying to extract certain words from a file using regex in python but I am unable to get it. My original file looks like

List/VB 
[ the/DT flights/NNS ]
from/IN 

and I want the output to be

List VB 
the DT
flights NNS
from IN 

I wrote the following code:

import re

with open("in.txt",'r') as infile, open("out.txt",'w') as outfile: 
    for line in infile:
        if (re.match(r'(?:[\s)?(\w\\\w)',line)):
            outfile.write(line)

Upvotes: 0

Views: 1420

Answers (2)

sl1ng5h0t
sl1ng5h0t

Reputation: 54

import re

expr = re.compile("(([\w]+)\/([\w]+))", re.M)
fp = open("file_list.txt",'r')
lines = fp.read()
fp.close()
a = expr.findall(lines)
for el in expr.findall(lines):
    print ' '.join(el[1:])

Outputs:

List VB
the DT
flights NNS
from IN

Upvotes: 0

Ali-Akber Saifee
Ali-Akber Saifee

Reputation: 4596

with the sample data you provided:

>>> data = """List/VB 
... [ the/DT flights/NNS ]
... from/IN"""

>>> expr = re.compile("(([\w]+)\/([\w]+))", re.M)
>>> for el in expr.findall(data):
>>>     print el[1], el[2]
List VB
the DT
flights NNS
from IN

Upvotes: 2

Related Questions