我想從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)
我的輸出打印爲陣列,如何使一個字符串? – 2013-02-26 00:47:34
你的意思是你想要將el [1]和el [2]轉換成單個字符串嗎?在這種情況下,你可以做s =「%s%s」%el [1:3] – 2013-02-26 01:11:28