2017-04-06 52 views
0

我有一个文本文件,像这样,我想处理它在PythonPython的商店一行行从文本文件列表

info.txt

firstname1 
    surname1 
    [email protected] 
    student1 
------------------- 
    firstname2 
    surname2 
    [email protected] 
    student2 
----------------- 

我想写一Python代码这iterares并存储在每个指数法示例的每一行:[firstname,surname,[email protected],student]并忽略"-----"

Python代码

with open('log.txt') as f: 
     lines = f.read().splitlines() 
     x = x + 1 

    for i in lines: 
     print i 

,但我认为这是不对的我AMM很新的蟒蛇可以有一个人请点我在正确的方向 我想输出到我的财产以后,像这样

输出

index 1 :first name: firstname1 
     Surname: surname1 
     Email: [email protected] 
     Student student1 

index 2 :first name: firstname2 
     Surname: surname2 
     Email: [email protected] 
     student: student2 
+0

请更具体地关于你想要的输出/结果。另外,'x'应该在你的代码中做什么? – timgeb

+1

更新了问题 – Craftx398

回答

1

我知道这将是更好的形式来解释如何做这样的一般准则,但对于这样一个简单的任务,代码自己说话,真的...

我会这样实现它。

from pprint import pprint # For nicer formatting of the output. 

# For the sake of a self-contained example, 
# the data is inlined here. 
# 
# `f` could be replaced with `open('log.txt'). 

f = """ 
    firstname1 
    surname1 
    [email protected] 
    student1 
------------------- 
    firstname2 
    surname2 
    [email protected] 
    student2 
----------------- 
""".splitlines() 

data = [] 
current = None 
for line in f: 
    line = line.strip() # Remove leading and trailing spaces 
    if not line: # Ignore empty lines 
     continue # Skip the rest of this iteration. 
    if line.startswith('-----'): # New record. 
     current = None # Clear the `current` variable 
     continue # Skip the rest of the iteration 
    if current is None: # No current entry? 
     # This can happen either after a ----- line, or 
     # when we're dealing with the very first line of the file. 

     current = [] # Create an empty list, 
     data.append(current) # and push it to the list of data. 
    current.append(line) 

pprint(data) 

输出是一个列表的列表:

[['firstname1', 'surname1', '[email protected]', 'student1'], 
['firstname2', 'surname2', '[email protected]', 'student2']] 
0

这里的,可能是更优雅一点的解决方案。 (只要你的文件严格保持从你的榜样的格式,即数据后跟一个虚线的四条线。)

from itertools import izip # skip this line if you are using Python 3 

with open('info.txt') as f: 
    result = [{'first name': first.strip(), 'Surname': sur.strip(), 
       'Email': mail.strip(), 'student': stud.strip()} 
       for first, sur, mail, stud, _ in izip(*[f]*5)] 

这给你的词典列表如下:

[{'first name': 'firstname1', 'Surname': 'surname1', 'Email': '[email protected]', 'student': 'student1'}, {'first name': 'firstname2', 'Surname': 'surname2', 'Email': '[email protected]', 'student': 'student2'}] 

如果您的“索引1”对应于列表的第一个元素(即result[0]),则“索引2”对应于列表的第二个元素,依此类推。

例如,你可以得到你index == 2用的姓氏:

index = 2 
result[index - 1]['Surname'] 

如果你真的困扰,该指数被转移,你可以建立从结果的字典。演示:

>>> result = dict(enumerate(result, 1)) 
>>> result 
{1: {'first name': 'firstname1', 'Surname': 'surname1', 'Email': '[email protected]', 'student': 'student1'}, 2: {'first name': 'firstname2', 'Surname': 'surname2', 'Email': '[email protected]', 'student': 'student2'}} 
>>> 
>>> result[2]['Surname'] 
'surname2' 
>>> 
>>> for index, info in result.items(): 
...  print index, info['first name'], info['Surname'], info['Email'], info['student'] 
... 
1 firstname1 surname1 [email protected] student1 
2 firstname2 surname2 [email protected] student2