0

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)
4

2 に答える 2

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
于 2013-02-26T00:03:30.547 に答える