阅读和存储Python行的特定部分

发布于 2025-02-07 03:33:44 字数 353 浏览 2 评论 0原文

我正在尝试按行读取此文本文件,并在我的程序中使用该线路中的特定信息以使用它。有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不知在何时 2025-02-14 03:33:44

你尝试过吗?

my_list = []

with open('my_text.txt') as f:
    lines = f.readlines()
    for line in lines:
        my_list.append(line.split('_')[0])    #index [0] to get the first element

my_list = list(set(my_list))    #get only unique names
print(my_list)

输出

['Shahran', 'Shahrake Gharb', 'Shahrake Qods', 'Pardis']

Have you tried this?

my_list = []

with open('my_text.txt') as f:
    lines = f.readlines()
    for line in lines:
        my_list.append(line.split('_')[0])    #index [0] to get the first element

my_list = list(set(my_list))    #get only unique names
print(my_list)

Output

['Shahran', 'Shahrake Gharb', 'Shahrake Qods', 'Pardis']
缺⑴份安定 2025-02-14 03:33:44

您必须在_上以_:拆分。您可能希望将所有信息存储为dict> dict s的list

请务必在代码> 方法。

records = []

with open('locations.txt') as f:
    for line in f:
        column1, column2, column3, remaining = line.split("_")
        column4, column5 = remaining.split(":")
        record = {}
        record["column1"] = column1
        record["column2"] = column2
        record["column3"] = column3
        record["column4"] = column4
        record["column5"] = column5
        records.append(record)

记录现在是由6 dict s组成的列表。要到达存储的字符串,您可以这样访问它们:

print(records[0]["column1"])

输出:

Shahran

You've got to split each line at _ and :. You probably want to store all the information as a list of dicts.

Be sure to read the documentation on the str.split method.

records = []

with open('locations.txt') as f:
    for line in f:
        column1, column2, column3, remaining = line.split("_")
        column4, column5 = remaining.split(":")
        record = {}
        record["column1"] = column1
        record["column2"] = column2
        record["column3"] = column3
        record["column4"] = column4
        record["column5"] = column5
        records.append(record)

records is now a list made up of 6 dicts. To get to the strings stored within, you can access them like so:

print(records[0]["column1"])

Output:

Shahran
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文