-1

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]    
Shinomoto Asakura
  • 1,473
  • 7
  • 25
  • 45
  • 2
    How about showing us the part of the program that's causing the Exception? Please show how you're opening your files/streams and how you're reading from/writing to them. – Tim Pietzcker Jun 03 '17 at 19:51
  • What do you mean by "It's returning accent"? The statement is an assignment which you say raises an exception. Assignments don't return anything. Statements that raise an exception can't return anything. – Stop harming Monica Jun 03 '17 at 20:14
  • Acute accent, words "é", "há" are examples, that kind of characters are returning – Shinomoto Asakura Jun 03 '17 at 20:18

1 Answers1

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).

Błotosmętek
  • 12,717
  • 19
  • 29
  • 1
    sorry, but how am I supposed to do it? – Shinomoto Asakura Jun 03 '17 at 20:03
  • I can not modify anything about PostgreSQL. – Shinomoto Asakura Jun 03 '17 at 21:50
  • 1
    Ah, so you already have LATIN1 in your Postgres database, but you haven't told Python that is is LATIN1, so Python is assuming UTF-8 instead. Call `conn.set_client_encoding('LATIN1')` before `cur.execute` and it should decode correctly. – Błotosmętek Jun 04 '17 at 13:39
  • Even I called `conn.set_client_encoding('LATIN1')` before cur.. Python's still assuming UTF-8, making impossible I see the "accent characters". Error `UnicodeDecodeError: 'utf8'` continues – Shinomoto Asakura Jun 04 '17 at 22:38
  • I found out, https://stackoverflow.com/questions/14881467/encoding-on-postgresql-python-jinja2.. What was missing it was these psycopg2.extensions imports. Thanks Błotosmętek – Shinomoto Asakura Jun 04 '17 at 23:00