2013-02-25 241 views
0

我想從python中使用正則表達式提取某些文字,但我無法得到它。我原來的文件看起來像Python文件正則表達式匹配

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

,我想輸出是

List VB 
the DT 
flights NNS 
from IN 

我寫了下面的代碼:

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) 

回答

2

與您提供的樣本數據:

>>> 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 
+0

我的輸出打印爲陣列,如何使一個字符串? – 2013-02-26 00:47:34

+0

你的意思是你想要將el [1]和el [2]轉換成單個字符串嗎?在這種情況下,你可以做s =「%s%s」%el [1:3] – 2013-02-26 01:11:28

0
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:]) 

輸出:

List VB 
the DT 
flights NNS 
from IN 
+0

你應該制定你的答案。 – Beppe 2013-09-21 21:29:00