【发布时间】:2009-06-08 23:26:03
【问题描述】:
大家好
我是 python 和编程的新手。我需要读取大文本文件的块,格式如下所示:
<word id="8" form="hibernis" lemma="hibernus1" postag="n-p---nb-" head-"7" relation="ADV"/>
我需要form、lemma 和postag 信息。例如以上我需要hibernis、hibernus1和n-p---nb-。
我如何告诉python读取直到它到达表单,向前读取直到到达引号",然后读取引号"hibernis"之间的信息?真的在为此苦苦挣扎。
到目前为止,我的尝试是删除标点符号,拆分句子,然后从列表中提取我需要的信息。虽然无法让 python 遍历整个文件,但我只能让它工作 1 行。我的代码如下:
f=open('blank.txt','r')
quotes=f.read()
noquotes=quotes.replace('"','')
f.close()
rf=open('blank.txt','w')
rf.write(noquotes)
rf.close()
f=open('blank.txt','r')
finished = False
postag=[]
while not finished:
line=f.readline()
words=line.split()
postag.append(words[4])
postag.append(words[6])
postag.append(words[8])
finished=True
感谢任何反馈/批评
谢谢
【问题讨论】:
标签: python