如何将txt文件中的行分组到嵌套字典中(Python)

发布于 2025-01-19 22:29:39 字数 598 浏览 0 评论 0原文

我有一个txt文件,其中包含如下数据:

Id_1 1111
Member_a 2222
Member_b 3333
Member_c 4444
Device_a 5555
Device_b 6666

Id_2 1234
Member_a 5678
Member_b 1345
Device_a 3141

Id_3 1317
Member_a 5643
Member_b 4678
Member_c 4654
Member_e 4674
member_f 2314
Device_a 4433
Device_b 4354

.
.
.

and so on

每个id包含3个字段,每个字段都有不同数量的子字段(例如,有些id有4个成员,但有些只有2个成员)是否有有什么方法可以将这些数据组合成嵌套字典之类的东西吗?

这是预期的输出:

{
 'id': 'Id_1', 
 'member': [{'Member_a': 2222}, {'Member_b': 3333}, {'Member_c': 4444}],
 'Device' : [{'Device_a': 5555}, {'Device_b': 6666}]
}

提前谢谢您!

I have a txt file that contain the data as below:

Id_1 1111
Member_a 2222
Member_b 3333
Member_c 4444
Device_a 5555
Device_b 6666

Id_2 1234
Member_a 5678
Member_b 1345
Device_a 3141

Id_3 1317
Member_a 5643
Member_b 4678
Member_c 4654
Member_e 4674
member_f 2314
Device_a 4433
Device_b 4354

.
.
.

and so on

There are 3 fields contained in each id and each field has a different number of sub-field (for example, some id has 4 members, but some have only 2 members) Is there any way that I could combine these data into something like a nested dictionary?

Here is the expected output:

{
 'id': 'Id_1', 
 'member': [{'Member_a': 2222}, {'Member_b': 3333}, {'Member_c': 4444}],
 'Device' : [{'Device_a': 5555}, {'Device_b': 6666}]
}

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

多彩岁月 2025-01-26 22:29:39

在下面的代码中,“ blankpaper.txt”包含您提供的文本。我构建了符合预期输出的字典,并将每个输出存储在列表dicts中。

在我的版本中,我已经在最终输出较低的情况下制作了所有字符串,尽管您可以根据自己的需求相应地调整代码。我不确定您的数据文件有时是否有例如成员_F在一行中,而成员_F在另一行上,或者这只是上面的错别字。如果已知类似的信息,则可以使代码更有效。

dicts = []

with open("blankpaper.txt") as f:
    for line in f:
        # if line not empty
        if (split := line.split()):
            a = split[0].lower()
            b = a[0:a.rfind('_')]
            if "id" == b:
                temp = {'id': a, 'member': [], 'device': []}
                dicts.append(temp)
            elif "member" == b:
                temp['member'].append({a: int(split[1])})
            elif "device" == b:
                temp['device'].append({a: int(split[1])})

print(dicts)

输出

[{'id': 'id_1', 'member': [{'member_a': 2222}, {'member_b': 3333}, {'member_c': 4444}], 'device': [{'device_a': 5555}, {'device_b': 6666}]}, {'id': 'id_2', 'member': [{'member_a': 5678}, {'member_b': 1345}], 'device': [{'device_a': 3141}]}, {'id': 'id_3', 'member': [{'member_a': 5643}, {'member_b': 4678}, {'member_c': 4654}, {'member_e': 4674}, {'member_f': 2314}], 'device': [{'device_a': 4433}, {'device_b': 4354}]}]

我希望这会有所帮助。如果有任何疑问,或者这不是您想要的,请告诉我。

In the code below, "blankpaper.txt" contains the text provided from you. I have constructed dictionaries that conform to the expected output and stored each one in the list dicts.

In my version, I have made all strings in the final output lower case though you can adjust the code accordingly based on your needs. I was not sure if your data file sometimes had for example, member_f, on one line, and Member_f on another line, or if that was just a typo above. If similar information is known, the code can be made more efficient.

dicts = []

with open("blankpaper.txt") as f:
    for line in f:
        # if line not empty
        if (split := line.split()):
            a = split[0].lower()
            b = a[0:a.rfind('_')]
            if "id" == b:
                temp = {'id': a, 'member': [], 'device': []}
                dicts.append(temp)
            elif "member" == b:
                temp['member'].append({a: int(split[1])})
            elif "device" == b:
                temp['device'].append({a: int(split[1])})

print(dicts)

Output

[{'id': 'id_1', 'member': [{'member_a': 2222}, {'member_b': 3333}, {'member_c': 4444}], 'device': [{'device_a': 5555}, {'device_b': 6666}]}, {'id': 'id_2', 'member': [{'member_a': 5678}, {'member_b': 1345}], 'device': [{'device_a': 3141}]}, {'id': 'id_3', 'member': [{'member_a': 5643}, {'member_b': 4678}, {'member_c': 4654}, {'member_e': 4674}, {'member_f': 2314}], 'device': [{'device_a': 4433}, {'device_b': 4354}]}]

I hope this helps. If there are any questions or if this is not exactly what you wanted, please let me know.

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