I have the latest QGIS 2.18.6 and i try to use some postgis query using python in QGS python console.
field characters from mytable is my non-English language and in postgis have encode 'utf-8' and any time where I want to work In QGIS never haven't problem with characters encode.
but any time to use python and postgis I have any time problem with encode characters.
here the code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from qgis.PyQt.QtXml import QDomDocument
from qgis.utils import iface
import psycopg2
constring = u"dbname='database' host='postgre' port='port' user='username' password='code'"
conn=psycopg2.connect(constring)
checkCur = conn.cursor()
sqlCheck = "select * from public.mytable;"
checkCur.execute(sqlCheck)
sqlCheckRess = checkCur.fetchall()
mylist=[]
for i in sqlCheckRess :
mylist.append(i)
checkCur.close()
conn.close()
print mylist
that work nice but that I take in print mylist is un-humanity language
print like this :
u'2247_\u03a5\u06c6\u03b9\u04c3\u03c4\u03ac\u05bc\u03b5\u03bd\u03bf \u03c5\u03c0\u03cc\u03b2\u03b1
1 Answer 1
Try this:
mylist=[]
for i in sqlCheckRess :
mylist.append([x.encode("utf-8") for x in i])
you are appending unicode objects to the list, that's why you see those un-human characters, have a look at this. Also, I assumed that "i" is a list when you append it.
-
@xDias not work I get error
no attribute to encode
.any other idea ?Mar– Mar2017年10月10日 13:48:24 +00:00Commented Oct 10, 2017 at 13:48 -
are you getting something like
'list' object has no attribute 'encode'
? if so somehow youri
object is a list rather than a unicode object. You can try encoding every item on thei
list, like this[[x.encode("utf-8") for x in y] for y in i]
. Be aware that by doing it this way you'll end up appending a list object tomylist
xlDias– xlDias2017年10月11日 14:00:44 +00:00Commented Oct 11, 2017 at 14:00 -
I try some error
no attribute to encode
Mar– Mar2017年10月17日 13:08:38 +00:00Commented Oct 17, 2017 at 13:08
s = u'2247_\u03a5\u06c6\u03b9\u04c3\u03c4\u03ac\u05bc\u03b5\u03bd\u03bf \u03c5\u03c0\u03cc\u03b2\u03b1'
andprint s
return me2247_ΥۆιӃτάּενο υπόβα
It's just how the string it's store.