我的输入文件为:
1 sentences, 6 words, 1 OOVs
1 zeroprobs, logprob= -21.0085 ppl= 15911.4 ppl1= 178704
6 words, rank1= 0 rank5= 0 rank10= 0
7 words+sents, rank1wSent= 0 rank5wSent= 0 rank10wSent= 0 qloss= 0.925606 absloss= 0.856944
file input.txt : 1 sentences, 6 words, 1 OOVs
1 zeroprobs, logprob= -21.0085 ppl= 15911.4 ppl1= 178704
6 words, rank1= 0 rank5= 0 rank10= 0
7 words+sents, rank1wSent= 0 rank5wSent= 0 rank10wSent= 0 qloss= 0.925606 absloss= 0.856944
我想提取单词ppl及其后面的值,在这种情况下:ppl = 15911.4
我正在使用此代码:
with open("input.txt") as openfile:
for line in openfile:
for part in line.split():
if "ppl=" in part:
print part
然而,这只是提取单词ppl而不是值。我还想打印文件名。
预期产出:
input.txt, ppl=15911.4
我该如何解决这个问题?
答案 0 :(得分:6)
您可以使用enumerate
功能,
list2
示例:
with open("input.txt") as openfile:
for line in openfile:
s = line.split()
for i,j in enumerate(s):
if j == "ppl=":
print s[i],s[i+1]
仅打印第一个值
>>> fil = '''1 zeroprobs, logprob= -21.0085 ppl= 15911.4 ppl1= 178704
6 words, rank1= 0 rank5= 0 rank10= 0'''.splitlines()
>>> for line in fil:
s = line.split()
for i,j in enumerate(s):
if j == "ppl=":
print s[i],s[i+1]
ppl= 15911.4
>>>
答案 1 :(得分:2)
您可以使用简单的计数器修复它:
found = False
with open("input.txt") as openfile:
for line in openfile:
if not found:
counter = 0
for part in line.split():
counter = counter + 1
if "ppl=" in part:
print part
print line.split()[counter]
found = True
答案 2 :(得分:0)
您可以将从line.split()
生成的列表分配给变量,然后使用while循环,使用i作为计数器进行迭代,当您点击'ppl ='时,您可以返回'ppl ='和下一个索引
with open("input.txt") as openfile:
for line in openfile:
phrases = line.split()
i = 0
while i < len(phrases):
if 'ppl=' in phrases[i]
print "ppl= " + str(phrases[i + 1])
i += 1