我有一个像这样的文本文件,我想在python中处理它
info.txt
firstname1
surname1
email@email.com1
student1
-------------------
firstname2
surname2
email@email.com2
student2
-----------------
我想编写一个python代码,迭代并存储每个索引示例中的每一行:[firstname,surname,email@email.com,student]
并忽略"-----"
python代码
with open('log.txt') as f:
lines = f.read().splitlines()
x = x + 1
for i in lines:
print i
但我相信这是错误的我对python非常新,有些人请指出我正确的方向 我希望输出给我这样的东西
输出
index 1 :first name: firstname1
Surname: surname1
Email: email@email.com1
Student student1
index 2 :first name: firstname2
Surname: surname2
Email: email@email.com2
student: student2
答案 0 :(得分: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@email.com1
student1
-------------------
firstname2
surname2
email@email.com2
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@email.com1', 'student1'],
['firstname2', 'surname2', 'email@email.com2', 'student2']]
答案 1 :(得分: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@email.com1', 'student': 'student1'}, {'first name': 'firstname2', 'Surname': 'surname2', 'Email': 'email@email.com2', '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@email.com1', 'student': 'student1'}, 2: {'first name': 'firstname2', 'Surname': 'surname2', 'Email': 'email@email.com2', '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@email.com1 student1
2 firstname2 surname2 email@email.com2 student2