UnicodeDecodeError: ascii codec cannot decode bytes at position: serial number not in range (128)

I have worked a bit on this error and cannot understand what is happening. As far as I understand, I mostly have problems because I am moving from one type of coding to another.

def write_table_to_file(table, connection):
    db_table = io.StringIO()
    cur = connection.cursor()
    #pdb.set_trace()
    cur.copy_to(db_table, table)
    cur.close()
    return db_tabl

This is the method that gives me headaches. The error below is output when I run this method

[u350932@config5290vm0 python3]$ python3 datamain.py 
Traceback (most recent call last):
  File "datamain.py", line 48, in <module>
    sys.exit(main())
  File "datamain.py", line 40, in main
    t = write_table_to_file("cms_jobdef", con_tctmsv64)
  File "datamain.py", line 19, in write_table_to_file
    cur.copy_to(db_table, table)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 40: ordinal not in range(128)

Client encoding in database, getting table from

tctmsv64=> SHOW CLIENT_ENCODING;
 client_encoding
-----------------
 sql_ascii
(1 row)

Database Encoding - LATIN1

Encoding the database on which I put them

S104838=# SHOW CLIENT_ENCODING;
 client_encoding
-----------------
 WIN1252
(1 row)

Database Encoding - UTF8

Of the threads I found, they recommend changing the encoding

To correct your function, you'll have to know what encoding the byte
string is in, and convert it to unicode using the decode() method,
and compare that result to the unicode string.

http://www.thecodingforums.com/threads/unicodedecodeerror-ascii-codec-cant-decode-byte-0xa0-in-position-10-ordinal-not-in-range-128.336691/

, , , . python 3.4 io.StringIO(initial_value = ', newline ='\n ') ¶, .

, , , , .

https://wiki.python.org/moin/UnicodeDecodeError

, , , . .

+4
2

Python 3 - - , IMO. Python 3.

, psycopg2 , , , , ( ) ascii .

io.BytesIO StringIO , copy_from .

, - , SQL_ASCII.

+1

. , , . , ! googling

https://docs.python.org/3/howto/unicode.html

StreamRecorder, . .

def write_table_to_file(table, connection):
    db_table = io.BytesIO()
    cur = connection.cursor()
    cur.copy_to(codecs.StreamRecoder(db_table,codecs.getencoder('utf-8'), codecs.getdecoder('latin-1'),
                                     codecs.getreader('utf-8'), codecs.getwriter('utf-8')), table)
    cur.close()
    return db_table

, -1 utf-8 , , . :)

+1

Source: https://habr.com/ru/post/1545936/


All Articles