我读了this question的答案,仍然得到错误AttributeError: 'dict' object has no attribute 'encode'
。在
我试过了
dic = pickle.load(fileObject)
for v in dic:
v.encode('ascii', 'ignore')
以及
^{pr2}$但还是有同样的错误。当打印出变量时,它们都在前面显示一个u
。字典在python3下被pickle,在python2中被取消pickle。在
我试了pp.pprint((dataFromPrevMod).encode('ascii', 'ignore'))
但没用。在
如果我pprint
出字典,它会显示它的内容,但是在python3中,每一行都以u
开头,例如u'website': u'exmample.org'
用python3打印的字典
{
'output': {
'table': 'intersection',
'file_location': '\\\\storage1\\tpn\\tpn_team\\dev\\asmithe\\',
'schema': 'asmithe',
'temporary_location': '\\\\storage1\\tpn\\tpn_team\\dev\\asmithe\
\'
},
'tpn_inventory_db_r': {
'generic_pwd': '51f3tlNE26',
'db_name': 'tpn',
'user': 'asmithe',
'schema': 'asmithe',
'host': 'example.tpns.org'
},
'proj_year': '2005',
'proj_rules_r': 'C:\\asmithe\\rules.txt',
'incidents_db_r': {
'schema': 'tpn_pp_dist',
'generic_pwd': '51f3tlNE26',
'db_name': 'tpn',
'user': 'asmithe',
'fire_table': 'incident',
'host': 'example.tpns.org'
},
'plots_to_project_r': 'C:\\Users\\asmithe\\Plots.txt',
'tpn_proj_db_r': {
'generic_pwd': '51f3tlNE26',
'db_name': 'tpn inventory',
'user': 'asmithe',
'schema': 'test',
'host': 'example.tpns.org'
}
}
用python2打印的字典(注意u的添加)
{ u'incidents_db_r': { u'db_name': u'tpn',
u'fire_table': u'incident',
u'generic_pwd': u'51f3tlNE26',
u'host': u'example.tpns.org',
u'schema': u'tpn_pp_dist',
u'user': u'asmithe'},
u'tpn_inventory_db_r': { u'db_name': u'tpn',
u'generic_pwd': u'51f3tlNE26',
u'host': u'example.tpns.org',
u'schema': u'asmithe',
u'user': u'asmithe'},
u'tpn_proj_db_r': { u'db_name': u'tpn inventory',
u'generic_pwd': u'51f3tlNE26',
u'host': u'example.tpns.org',
u'schema': u'test',
u'user': u'asmithe'},
u'output': { u'file_location': u'\\\\storage1\\tpn\\tpn_team\\dev\\asmithe\ \',
u'schema': u'asmithe',
u'table': u'intersection',
u'temporary_location': u'\\\\storage1\\tpn\\tpn_team\\dev\\asmithe idek\\'},
u'plots_to_project_r': u'C:\\Users\\asmithe\\Plots.txt',
u'proj_rules_r': u'C:\\asmithe\\rules.txt',
u'proj_year': u'2005'}
你有嵌套的dicts,所以你需要这样的东西(快速完成只是为了给你一个想法):
但这不是在处理钥匙。在
希望有帮助。在
pprint
将unicode字符串显示为u'bla'
的原因是,在python2中,string
和{因此,Python2中的
pprint
没有这样显示它们是合乎逻辑的。在为了完整性起见:编写一个自定义pickler来自动将unicode对象编码为字符串是非常复杂的。以下仅适用于Python 2:
相关问题 更多 >
编程相关推荐