2017-06-20 31 views
2

我正在尝试使用Python解析/处理来自文本文件的一些信息。该文件包含姓名,员工编号和其他数据。我不提前知道姓名或员工数量。我知道名称后面有文本:“Per End”,在员工编号之前有文本“File:”。我可以使用.find()方法找到这些项目。但是,我如何要求Python查看“Per End”和“File:”之前或之后的信息?在这种特定情况下,输出应该是名称和员工编号。Python文本处理/查找数据

的文字是这样的:

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) 
+0

'enumerate'帮助您访问线和它的指数在同一时间 –

回答

1
file = open("Register.txt", "rt") 
lines = file.readlines() 
file.close() 

for indx, line in enumerate(lines): 
    line = line.strip() 
    print (line) 
    if line.find('Per End') != -1: 
     print lines[indx-1].strip() 
    if line.find('File:') != -1: 
     print lines[indx+1].strip() 

枚举(行)可以访问指标和线路,以及有由您可以访问上一行和下一行以及

这里是我的stdout直接在python shell中运行:

>>> file = open("r.txt", "rt") 
>>> lines = file.readlines() 
>>> file.close() 
>>> lines 
['SMITH, John\n', 'Per End: 12/10/2016\n', 'File:\n', '002013\n', 'Dept:\n', '000400\n', 'Rate:10384 60\n'] 

>>> for indx, line in enumerate(lines): 
...  line = line.strip() 
...  if line.find('Per End') != -1: 
...  print lines[indx-1].strip() 
...  if line.find('File:') != -1: 
...  print lines[indx+1].strip() 

SMITH, John 
002013 
+0

我试试这个代码,我得到这个错误: 类型错误:“builtin_function_or_method”对象没有属性'__getitem__' 这将需要运行一个文本文件与多个员工 –

+0

是的,这对我有一个文本文件与多个员工工作。我在问题 –

+0

上根据您的示例数据创建了示例文本文件。谢谢。但是,我怎样才能解决这个错误? “TypeError:'builtin_function_or_method'对象没有属性'__getitem__'” –

0

这是我该怎么做的。

首先是一些测试数据。

test = """SMITH, John\n 
Per End: 12/10/2016\n 
File:\n 
002013\n 
Dept:\n 
000400\n 
Rate:10384 60\n""" 

text = [line for line in test.splitlines(keepends=False) if line != ""] 

现在为真正的答案。

count_per, count_num = 0, 0 

在iterable上使用enumerate会自动给你一个索引。

for idx, line in enumerate(text): 

    # Just test whether what you're looking for is in the `str` 

    if 'Per End' in line: 
     print(text[idx - 1]) # access the full set of lines with idx 
     count_per += 1 
    if 'File:' in line: 
     print(text[idx + 1]) 
     count_num += 1 

print("Per Ends = {}".format(count_per)) 
print("Files = {}".format(count_num)) 

产量对我来说:

SMITH, John 
002013 
Per Ends = 1 
Files = 1 
+0

这很好,但我需要从包含多名员工的文本文件中提取数据。这是否允许我这样做,并在每次找到指定的字符串时打印此结果? –

+0

当然。只需从文件中读入原来的“行”,将'enumerate(text)'改为'enumerate(lines)'。我只是想提供测试数据,以便其他人可以跟随,尽管没有你的原始文件 - 我太训练有素了测试:) –