循环中的字典使用字符串列表 - Python

bad*_*ogs 0 python attributes dictionary

如何迭代多个词典列表?

list_country = ['dict_DAN', 'dict_FRA']

dict_DAN ={ 'APPROVEDTXT':  'GODKENDT', 'REVIEWTXT':  'KONTROLLERET AF'}
dict_FRA ={ 'APPROVEDTXT':  'Aprov', 'REVIEWTXT': 'Controllier'}

for country in list_country:
    print country

    for (key,item) in country.items:
            print key
            print item`
Run Code Online (Sandbox Code Playgroud)

这会抛出一个错误:文件"",第9行,在AttributeError中:'str'对象没有属性'items'

我希望代码在第一次迭代时使用dict_DAN,在第二次迭代时使用dict_FRA.怎么能实现这个?

Dan*_*man 7

除非你需要其他名称,list_country否则你应该把实际的dicts放在那里:

dict_DAN ={ 'APPROVEDTXT':  'GODKENDT', 'REVIEWTXT':  'KONTROLLERET AF'}
dict_FRA ={ 'APPROVEDTXT':  'Aprov', 'REVIEWTXT': 'Controllier'}

list_country = [dict_DAN, dict_FRA]
Run Code Online (Sandbox Code Playgroud)

或者,你可以自己制作一个词典:

list_country = {'dict_DAN': dict_DAN, 'dict_FRA': dict_FRA]

for name, country in list_country.items():
    ...
Run Code Online (Sandbox Code Playgroud)