[英]convert text file into dictionary of dictionaries
I am having issues I have seen problem converting text files into a dictionary, but I think the issue is that the text file is a dictionary of dictionaries which I cannot seem to find a solution for.我遇到了问题我已经看到将文本文件转换为字典时出现问题,但我认为问题在于文本文件是字典的字典,我似乎无法找到解决方案。
Code:代码:
# importing the module
import ast
# reading the data from the file
with open('emoji_data.txt', encoding="utf8") as f:
data = f.read()
print("Data type before reconstruction : ", type(data))
data = ast.literal_eval(data)
print("Data type before reconstruction : ", type(data))
Error:错误:
ValueError: malformed node or string: <ast.Name object at 0x0000020FBD4345E0>
.txt File: .txt 文件:
EMOJI_DATA = {
u'\U0001F947': { # 🥇
'en' : ':1st_place_medal:',
'status' : fully_qualified,
'E' : 3,
'de': ':goldmedaille:',
'es': ':medalla_de_oro:',
'fr': u':médaille_d’or:',
'pt': ':medalha_de_ouro:',
'it': u':medaglia_d’oro:',
'fa': u':مدال_طلا:'
},
u'\U0001F948': { # 🥈
'en' : ':2nd_place_medal:',
'status' : fully_qualified,
'E' : 3,
'de': ':silbermedaille:',
'es': ':medalla_de_plata:',
'fr': u':médaille_d’argent:',
'pt': ':medalha_de_prata:',
'it': u':medaglia_d’argento:',
'fa': u':مدال_نقره:'
},
u'\U0001F949': { # 🥉
'en' : ':3rd_place_medal:',
'status' : fully_qualified,
'E' : 3,
'de': ':bronzemedaille:',
'es': ':medalla_de_bronce:',
'fr': u':médaille_de_bronze:',
'pt': ':medalha_de_bronze:',
'it': ':medaglia_di_bronzo:',
'fa': u':مدال_برنز:'
},
A couple of comments looking at the text file:查看文本文件的一些评论:
If the text file you included captures all the text, then the last line needs to end in another '}', not a comma.如果您包含的文本文件包含所有文本,那么最后一行需要以另一个“}”结尾,而不是逗号。
ast.literal_eval() does not work with python variable aliases.The text file includes this 'fully_qualified' alias: ast.literal_eval() 不适用于 python 变量别名。文本文件包含此“fully_qualified”别名:
u'\U0001F947': { # 🥇
'en' : ':1st_place_medal:',
'status' : fully_qualified,
This variable alias won't work with ast.literal_eval().此变量别名不适用于 ast.literal_eval()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.