-3

我编写了一个函数来从文件中提取模式,但它不能正常工作。为什么?

我在Python中的代码:

import re

f = open('quran_final.txt','r')
r = open('q.txt','w')
p = re.compile('ROOT:(?P<root>[^ "]+)')
for line in f:
    root = p.findall(line)
    if(root):
        for word in root:
        print word
    else:
        print ' '

我认为问题出在“检查”中,但是我应该如何更改呢?

我的文本文件示例:

f:CONJ+ POS:V IMPV ROOT:Akl 2MP  
POS:LOC LEM:Hayov ROOT:Hyv  
POS:V PERF ROOT:$yA 2MP  
POS:ADJ LEM:ragad ROOT:rgd M INDEF ACC  
wa+ POS:V IMPV ROOT:dxl 2MP  
Al+ POS:N LEM:baAb ROOT:bwb M ACC  
POS:N LEM:saAjid ROOT:sjd MP INDEF ACC  
wa+ POS:V IMPV ROOT:qwl 2MP  
POS:N LEM:HiT~ap ROOT:HTT F INDEF NOM  
POS:V IMPF ROOT:gfr 1MP MOOD:JUS  
POS:N LEM:xaTiy^_#ap ROOT:xTA P ACC PRON:2MP  
wa+ sa+ POS:V IMPF ROOT:zyd 1MP  
Al+ POS:N ACT PCPL (IV) LEM:muHosin ROOT:Hsn MP ACC  
f:REM+ POS:V PERF (II) ROOT:bdl 3MS  
POS:V PERF ROOT:Zlm 3MP  
POS:N LEM:qawol ROOT:qwl M INDEF ACC  
POS:REL LEM:{l~a*iY MS  
POS:V PERF PASS ROOT:qwl 3MS  
f:REM+ POS:V PERF (IV) ROOT:nzl 1MP  
POS:REL LEM:{l~a*iY MP  
POS:V PERF ROOT:Zlm 3MP  
POS:N LEM:rijoz ROOT:rjz M INDEF ACC  
Al+ POS:N LEM:samaA^' ROOT:smw F GEN  
POS:V PERF ROOT:kwn SP:kaAn 3MP  
POS:V IMPF ROOT:fsq 3MP  
POS:V PERF (X) ROOT:sqy 3MS  
4

2 回答 2

1

如果我理解正确,您需要在模式中包含 endline

p = re.compile('ROOT:(?P<root>[^\s]+)')
于 2013-02-15T15:59:57.703 回答
-2

缩进 - 打印单词需要再缩进四个空格。根应该是:

root = re.findall(p, line)
于 2013-02-16T03:27:10.867 回答