ฉันอ่านคำตอบของคำถามนี้ และยังคงได้รับข้อผิดพลาด AttributeError: 'dict' object has no attribute 'encode'
.
ฉันพยายามแล้ว
dic = pickle.load(fileObject)
for v in dic:
v.encode('ascii', 'ignore')
และ
dic = pickle.load(fileObject)
for key, val in dic.iteritems():
val.encode('ascii', 'ignore')
และยังคงได้รับข้อผิดพลาดเดิม เมื่อพิมพ์ตัวแปรทั้งหมดจะแสดงด้วย u
ที่ด้านหน้า พจนานุกรมถูกดองภายใต้ python 3 และกำลังไม่ถูกดองใน python 2
ฉันลอง pp.pprint((dataFromPrevMod).encode('ascii', 'ignore'))
แล้วมันไม่ได้ผล
ถ้าฉัน pprint
ออกจากพจนานุกรม มันจะแสดงเนื้อหา แต่ใน Python 3 แต่ละบรรทัดเริ่มต้นด้วย u
เช่น u'website': u'exmample.org'
พจนานุกรมพิมพ์สวยใน Python 3
{
'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'
}
}
พจนานุกรมสวยพิมพ์ใน Python 2 (สังเกตการเพิ่มของคุณ)
{ 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'}
v
อาจไม่ใช่ชื่อตัวแปรที่ดีที่สุดtype(key)
และtype(val)
คืออะไร? ขณะนี้ดูเหมือนว่าจะมีพจนานุกรมซ้อนกันอยู่ในนั้น - person dhke   schedule 20.08.2015print(val)
ได้ไหม? - person Anand S Kumar   schedule 20.08.2015data
ไม่ใช่สิ่งที่คุณคิด... ทำดีๆimport pprint
pprint.pprint(data)
- person Maresh   schedule 20.08.2015val.encode(whatever)
จะสร้างและส่งกลับ สตริงใหม่ ให้กับคุณ โดยปล่อยให้สตริงเดิมไม่มีการแก้ไข ในโค้ดนี้ คุณจะเพิกเฉยต่อค่าสตริงใหม่ซึ่งอาจไม่ต้องการตามที่คุณต้องการ - person bgporter   schedule 20.08.2015data
คืออะไรกันแน่ เพราะจากข้อผิดพลาดที่คุณรายงาน ดูเหมือนว่ามันจะเป็น dicts ที่ซ้อนกันจริงๆ - person Maresh   schedule 20.08.2015pprint
หรือprint
หรือtype
:) ดูความคิดเห็นก่อนหน้าของฉัน - person Maresh   schedule 20.08.2015u''
s ปรากฏที่ไหน? แน่นอนว่าจะปรากฏในrepr(u'äöü')
แต่print(u'äöü')
ควรถอดรหัสยูนิโค้ดอย่างถูกต้องไปยังเทอร์มินัลการเข้ารหัสที่เหมาะสม - person dhke   schedule 20.08.2015u
ดังนั้น OP จึงต้องการทำให้สตริง ไม่ใช่ Unicode - person TigerhawkT3   schedule 20.08.2015pprint(data)
? จากข้อความแสดงข้อผิดพลาดของคุณdata
ดูเหมือนว่า:{ 'one': {...} }
ดังนั้นคุณกำลังพยายามเรียก.encode
ด้วย dict ไม่ใช่สตริง นั่นคือสิ่งที่เราต้องการตรวจสอบ - person Maresh   schedule 20.08.2015isinstance
และself
- person Celeritas   schedule 22.08.2015