阅读和存储Python行的特定部分
我正在尝试按行读取此文本文件,并在我的程序中使用该线路中的特定信息以使用它。有5条信息,其中4个用“ _”分开。最后一个用“:”分开。
TXT文件示例:
Shahran_1_True_True:61666.67
Shahran_1_True_True:61666.67
Pardis_2_True_True:18333.33
Shahrake Qods_2_True_True:30083.33
Shahrake Gharb_2_True_True:233333.33
例如,我需要将Shahran,Pardis和Shahrake QOD存储在列表中。我可以阅读行,但我无法分开信息。谁能告诉我一种这样做的方式?
I'm trying to read this text file line by line and store specific information from the line to use it after in my program. There are 5 pieces of information and 4 of them are separated with "_". The last one is separated with ":".
Txt file sample:
Shahran_1_True_True:61666.67
Shahran_1_True_True:61666.67
Pardis_2_True_True:18333.33
Shahrake Qods_2_True_True:30083.33
Shahrake Gharb_2_True_True:233333.33
For example, I need to store Shahran, Pardis and Shahrake Qods in a list. I can read the lines but I can't separate the information. Can anyone show me a way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你尝试过吗?
输出
Have you tried this?
Output
您必须在
_
和:
上以_
:拆分。您可能希望将所有信息存储为dict> dict
s的list
。请务必在代码> 方法。
记录
现在是由6dict
s组成的列表。要到达存储的字符串,您可以这样访问它们:输出:
You've got to split each line at
_
and:
. You probably want to store all the information as alist
ofdict
s.Be sure to read the documentation on the
str.split
method.records
is now a list made up of 6dict
s. To get to the strings stored within, you can access them like so:Output: