2017-04-06 24 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 
相關問題