从纯文本文件导入字典 [英] import dict from plain text file
本文介绍了从纯文本文件导入字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我在文本文件中有字典,但是我丢失了创建它的python代码. 是否仍然可以从python查询信息到此字典? 如何创建一个能够读取文本sintaxis并创建dict结构的python脚本呢? 该文件很大,但是包含正确的python字典(列表,字典和元组的列表) 示例:
Let's say I have a dict in text file but I've lost the python code which created it. Is it still possible to query info from python to this dict? What about creating a python script capable of reading the text sintaxis and create the dict structure? The file is huge, but it contains a correct python dict (of list, dicts and tuples) Example:
{'CFGT': 'NONE',
'FALN_GPH': {},
'CDLAIN_ST': [],
'CISN_IF': {'MT_DK': 'MFO',
'MD_GP': 'indi',
就像一个泡菜字典,但易于阅读.
Is like a pickle dict, but human readable.
推荐答案
with open('dictionaryFile.txt','r') as filein:
dict_from_file = eval(filein.read())
这篇关于从纯文本文件导入字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文