Python - 将文本文件读入字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20411685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:22:08  来源:igfitidea点击:

Python - Reading a text file into dictionary

pythonfiletextdictionary

提问by user3071524

I recently started programming with Python and i've encountered quite a big problem with one of my functions. I'm trying to take data from a text file in the form of a list, into a dictionary in Python. The format of the text file is consistently formatted as is displayed below:

我最近开始使用 Python 进行编程,但我的一个函数遇到了相当大的问题。我正在尝试将列表形式的文本文件中的数据放入 Python 字典中。文本文件的格式一致,如下所示:

@text {
    n = "test1",
    r = ["B:1", "G:2", "H:3", "O:4", "S:5", "W:6"],
    t = ["G:1","H:2"]
}

Using the three keys: n, r and t; how would I go about reading their values from the text file into my dictionary using Python?

使用三个键:n、r 和 t;我将如何使用 Python 从文本文件中读取它们的值到我的字典中?

So far, I have managed to develop the following code with no success, not knowing where i've gone wrong despite attempting to research this all over the web.

到目前为止,我设法开发了以下代码但没有成功,尽管尝试在整个网络上进行研究,但不知道我哪里出错了。

f = open('text.txt', 'r')
newDict = {}
for line in f:
    n, r, t = line.strip().split('=')
    newDict[k.strip()] = v.strip()

Am I along the right lines with this or completely off the mark? The whole concept of reading multiple keys and values into a dictionary from a text file has me completely confused when it comes to the process of importing/converting the file.

我是在正确的路线上还是完全不合时宜?在导入/转换文件的过程中,将多个键和值从文本文件读入字典的整个概念让我完全困惑。

Any help with this would be greatly appreciated - thank you in advance.

对此的任何帮助将不胜感激 - 在此先感谢您。

回答by Steve P.

Not sure if this is the case, but if you're outputting said text file, and trying to read it back in later, you can use Pickleinstead of simply writing to a text file. This allows you to output an object and read it back in later, aka object serialization.

不确定是否是这种情况,但是如果您正在输出所述文本文件,并尝试稍后重新读取它,您可以使用Pickle而不是简单地写入文本文件。这允许您输出一个对象并在稍后读取它,也就是对象序列化。

As an example:

举个例子:

import pickle

#to save object
pickle.dump(yourDict, open(yourFile, 'wb'))

#to load it back:
yourDict = pickle.load(open(yourFile, 'rb'))

回答by Boo

You can do:

你可以做:

for line in f:
    listedline = line.strip().split('=') # split around the = sign
    if len(listedline) > 1: # we have the = sign in there
        newDict[listedline[0]] = listedline[1]

However, what do you want to do with the data stored in this dict? It will store everything as strings so your list will be a big string. If you need more refined data, it's not too hard but you will have to tell us what it is you want to accomplish with this dict.

但是,您想对存储在此 dict 中的数据做什么?它将所有内容存储为字符串,因此您的列表将是一个大字符串。如果您需要更精细的数据,这并不难,但您必须告诉我们您想用这个 dict 完成什么。

回答by alko

If you can't control your input text files, you can parse them with (potentially unsafe, so make sure of input) eval, see demo:

如果您无法控制您的输入文本文件,您可以使用 (可能不安全,因此请确保输入) 解析它们eval,请参阅演示:

source = """@text {
    n = "test1",
    r = ["B:1", "G:2", "H:3", "O:4", "S:5", "W:6"],
    t = ["G:1","H:2"]
}"""
nrt = ' '.join(source.splitlines()[1:4])

here nrtis space-joined lines with n, rand tdefinition. To make it valid python code, wrap with dict(..), and eval result:

nrt是带有n,rt定义的空格连接线。要使其成为有效的 Python 代码,请使用dict(..), 和 eval 结果进行包装:

obj_code = 'dict({})'.format(nrt)
result = eval(obj_code)

And finally:

最后:

>>> result
{'r': ['B:1', 'G:2', 'H:3', 'O:4', 'S:5', 'W:6'], 't': ['G:1', 'H:2'], 'n': 'test1'}

回答by Jon Clements

Here's a crude attempt:

这是一个粗略的尝试:

text = """
@text {
    n = "test1",
    r = ["B:1", "G:2", "H:3", "O:4", "S:5", "W:6"],
    t = ["G:1","H:2"]
}"""

import re
from collections import defaultdict
from ast import literal_eval

items = defaultdict(dict)
for name, lines in re.findall(r'(@\w+) {\s*(.*?)\s*}', text, flags=re.S):
    for var, val in re.findall(r'\s*(\w+)\s*=\s*(.*?),?$', lines, flags=re.M):
        items[name][var] = literal_eval(val)

# defaultdict(<type 'dict'>, {'@text': {'r': ['B:1', 'G:2', 'H:3', 'O:4', 'S:5', 'W:6'], 't': ['G:1', 'H:2'], 'n': 'test1'}})