将python字典从外部文件导入主文件时发生ast.literal\u eval错误

将python字典从外部文件导入主文件时发生ast.literal\u eval错误,python,dictionary,syntax-error,abstract-syntax-tree,Python,Dictionary,Syntax Error,Abstract Syntax Tree,到目前为止,我已经将字典转换成一系列列表,并查阅了文档和一些问题。我有点困惑,因为字典是ast.literal\u eval()可以处理的允许结构之一 外部字典文件内容: reports={ dict(DatabaseReports1='reports/dr_d1?'), dict(DatabaseReports2='reports/dr_d2?'), dict(DatabaseReports3='reports/dr_d3?'), dict(TitleReports='reports/tr?'

到目前为止,我已经将字典转换成一系列列表,并查阅了文档和一些问题。我有点困惑,因为字典是
ast.literal\u eval()
可以处理的允许结构之一

外部字典文件内容:

reports={
dict(DatabaseReports1='reports/dr_d1?'),
dict(DatabaseReports2='reports/dr_d2?'),
dict(DatabaseReports3='reports/dr_d3?'),
dict(TitleReports='reports/tr?'),
dict(BookReport1='reports/tr_b1?'),
dict(BookReport2='reports/tr_b2?'),
dict(BookReport3='reports/tr_b3?'),
dict(JournalReport1='reports/tr_j1?'),
dict(JournalReport2='reports/tr_j2?'),
}
主要文件内容:

with open('dictionaryFile.py', 'r') as f2:
        rs = f2.read()
        report=ast.literal_eval(rs)
我希望使用
ast
将文件从字符串转换为字典,但我得到以下错误:

Traceback (most recent call last):
  File "mainFile.py", line 4, in <module>
   __import__("reportOptions")
  File "dictionaryFile.py", line 11, in <module>
    dict(JournalReport2='reports/tr_j2?'),
TypeError: unhashable type: 'dict'
回溯(最近一次呼叫最后一次):
文件“mainFile.py”,第4行,在
__导入(“报告选项”)
文件“dictionaryFile.py”,第11行,在
dict(JournalReport2='reports/tr_j2?'),
TypeError:无法损坏的类型:“dict”

您定义了
dict
集合,这就是为什么会出现错误。改为这样定义一个
dict

此外,当使用
ast.literal\u eval()
时,您无法进行赋值,因此请摆脱
报告=

{
    'DatabaseReports': 'reports/dr_d1?',
    'DatabaseReports2': 'reports/dr_d2?',
    ...
}

您还需要摆脱
报告=