简体   繁体   English

将文本文件读入字典时出现问题

[英]Problems reading a text file into dictionary

I am a newbie in Python and would like to read a text file into a dictionary. 我是Python的新手,想将文本文件读入字典中。 The problem is that it reads in everything and only keeps the last record. 问题在于它读取所有内容,并且仅保留最后一条记录。 I want to read in all the data and store everything in a Python dict . 我想读取所有数据并将所有内容存储在Python dict

It is just a simple text file reading and python data dictionary storing. 它只是一个简单的文本文件读取和python数据字典存储。 Not sure why it does not work. 不知道为什么它不起作用。 Appreciate if someone can help. 感谢有人可以提供帮助。

book_data = {}

with open('test_data.txt', 'r', encoding='utf8') as raw_data:
        for item in raw_data:
            if ':' in item:
                key,value = item.split(':', 1)
                book_data[key]=value.lower()

test_data.txt test_data.txt

Book_ID: #111
Book_Title: Python 101
Book_description: This is a book about Python for beginners. 

Book_ID: #222
Book_Title: Java 101
Book_description: This is a book about  Java  for beginners. 


Book_ID: #333
Book_Title: Ruby 101
Book_description: This is a book about  Ruby for beginners. 


Book_ID: #444
Book_Title: C# 101
Book_description: This is a book about  C#  for beginners. 

My output is just one record instead of 4 records. 我的输出只是一个记录,而不是4个记录。

for k,v in book_data.items():
    print(k," : ", v)

Output: 输出:

Book_ID  :   #444

Book_Title  :   c# 101

Book_description  :   this is a book about  c#  for beginners.

You are overwriting each time, the key "Book_ID" for example, is first saved as 111, then 222, then 333 and then 444 by the time your print. 每次都覆盖,例如,在打印时,键“ Book_ID”首先被保存为111,然后是222,然后是333,然后是444。 You are probably using the wrong datastructure for your problem. 您可能为问题使用了错误的数据结构。 If you wanted to use the id as a key, you should probably create a new dict for each for the objects and insert them into book_data with the ID as key 如果要使用id作为键,则可能应该为每个对象创建一个新字典,然后将它们插入ID为键的book_data中

您每次迭代都会在book_data中重写相同键的值。

You're using three keys: Book_ID , Book_Title and Book_description . 您正在使用三个键: Book_IDBook_TitleBook_description A dict can hold only one value for a key. 字典只能容纳一个键的一个值。 You need to find a more suitable data structure to represent that file in memory. 您需要找到一个更合适的数据结构来表示该文件在内存中。

What you probably want is a dict where the keys are 111, 222, 333, 444. And the value for each key would be another dict with keys Book_ID , Book_Title and Book_description . 您可能想要的是一个键为Book_ID字典。每个键的值将是另一个键为Book_IDBook_TitleBook_description

Loop through the file, processing keys as you do now. 循环浏览文件,像现在一样处理密钥。 But whenever you encounter an empty line, put the dict you have collected up to that point into a parent dict. 但是,每当遇到空白行时,请将截至该点为止已收集的字典放入父字典中。 Then continue scanning with an empty dict. 然后继续扫描空的字典。 At the end of the file, put the last dict into the parent as well. 在文件末尾,将最后一个字典也放到父文件中。

Here's some skeleton code, taking into account the comments: 这是一些框架代码,考虑了注释:

book_stash = {}

with open(...) as raw_data:
        for item in raw_data:
            if ':' in item:
                key,value = item.split(':', 1)
                if key == 'Book_ID':
                   book_data = {}
                   book_stash[value] = book_data
                else:
                   book_data[key] = value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM