【发布时间】:2017-06-20 16:08:07
【问题描述】:
我正在尝试使用 Python 解析/处理文本文件中的一些信息。该文件包含姓名、员工编号和其他数据。我事先不知道姓名或员工编号。我知道在名字之后有文字:“Per End”,在员工编号之前有文字:“File:”。我可以使用 .find() 方法找到这些项目。但是,我如何让 Python 查看“Per End”和“File:”之前或之后的信息?在这种特定情况下,输出应该是姓名和员工编号。
文字如下:
SMITH, John
Per End: 12/10/2016
File:
002013
Dept:
000400
Rate:10384 60
我的代码是这样的:
file = open("Register.txt", "rt")
lines = file.readlines()
file.close()
countPer = 0
for line in lines:
line = line.strip()
print (line)
if line.find('Per End') != -1:
countPer += 1
print ("Per End #'s: ", countPer)
【问题讨论】:
-
enumerate帮助您同时访问线路及其索引