【发布时间】:2016-04-09 20:21:15
【问题描述】:
如果我有按保存顺序排列的单词文件,则新行来自下面
word 1
word 2
word 3
word 4
word 5
现在如果我有一些单词,例如“单词 3”,如果文本文件中存在“单词 3”,请按列表顺序显示“单词 3”中的上一个或下一个单词。
data = open('D:\path\file.txt', "r")
searchlines = data.readlines()
for q_numb, line in enumerate(searchlines):
if ('word 3') in line:
for l in searchlines:
prev_line = l[:+1]
print ('"word 3" found:',line, 'previous of "word 3" is:',prev_line)
break
data.close()
我的结果,但是错了,previous必须是“word 2”
"word 3" found: word 3
previous of "word 3" is: word 1
如果我想在 current 之后的下一个单词,如何加上 plus?
我的加号结果:
"word 3" found: word 3
previous of "word 3" is: w
我做错了什么?
【问题讨论】:
标签: python-3.x