Python: UnicodeDecodeError: 'utf8'

末鹿安然 提交于 2019-12-13 08:38:26

问题


I'm having problem to save accented letters. I'm using POSTGRESQL and Python 2.7

POSTGRESQL -  ENCODING = 'LATIN1'

I already added this line but does not worked!

#!/usr/bin/python
# -*- coding: UTF-8 -*-

More about error message:

UnicodeDecodeError: 'utf8' codec can't decode byte 0xed 

Please, any idea how to fix it?

@Edit:

cur = conn.cursor()
cur.execute("SELECT * FROM users")
rows = cur.fetchall()


obj_list = list()
for row in rows:
 ob = dict() 
 ob['ID'] = row[0]
 ob['NAME'] = row[1]
 ob['CITY'] = row[2]
 ob['USERNAME'] = row[3]

 obj_list.append(ob)

# print obj_list
# sys.exit()
def add_object(ob, row):
 ws.cell(column=3, row=row).value = ob['ID']
 ws.cell(column=4, row=row).value = ob['NAME']
 ws.cell(column=6, row=row).value = ob['CITY']
 ws.cell(column=8, row=row).value = ob['USERNANE'] 

This part of code is triggering the error. It's returning accent..

ob['CITY'] = row[2]    

回答1:


First thing to check is whether your "accented letters" belong to LATIN1 set - for example, á does, but ś doesn't. If not, you really should use UTF8 encoding in PostgreSQL (it is probably safer anyway).



来源:https://stackoverflow.com/questions/44347924/python-unicodedecodeerror-utf8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!