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()
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=
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
, , , . .