我有一个这样的文本文件,我想用python处理它
信息.txt
firstname1
surname1
email@email.com1
student1
-------------------
firstname2
surname2
email@email.com2
student2
-----------------
我想写一个python代码,它在每个索引中检索和存储每一行,例如:[firstname,surname,email@email.com,student]
,而忽略{
python代码
^{pr2}$但我相信这是错误的,我对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
我知道最好用这种形式来解释如何做这样的事情的一般准则,但是对于这样一个简单的任务,代码本身就说明了问题,真的。。。在
我会这样做的。在
输出是列表列表:
^{pr2}$这里有一个更优雅的解决方案。(只要您的文件严格遵守示例中的格式,即四行数据后面跟着一条虚线。)
这将为您提供如下词典列表:
^{pr2}$其中“index 1”对应于列表的第一个元素(即
result[0]
),“index 2”对应于列表的第二个元素,依此类推。在例如,您可以通过以下方式获得
index == 2
的姓氏:如果你真的为索引的移位而烦恼,你可以根据结果建立一个字典。演示:
相关问题 更多 >
编程相关推荐