Encoding while reading postgres data

42 views
Skip to first unread message

Yebach

unread,
Jan 4, 2016, 11:34:16 AM1/4/16
to web2py-users
Hello

I have the following scenario. 

Database is PostgreSQL, encoding set to UTF-8

When I read data from database with web2py I get a string reading in type of Bolni\xc4\x8dar where it was suppose to be Bolničar

Now, when i use web2py to present data in my view is all fine (so I can see Bolničar), but when i use Java Script or anything else to present data I get weird behavior, where ščžć is not presented.
I would like that  my reading from db would be Bolničar from beginning so I have no more hassle later.

also the data in my database is Bolničar.

I do all the db inserts with web2py and they are all ok. Looks like only reading is kind of "wrong"

Any suggestions?
Thank you

Richard Vézina

unread,
Jan 4, 2016, 11:39:32 AM1/4/16
to web2py-users
When passing data from python to javascript I guess you will need to use unicode or decode 

try 

javascritp_string_var = s.decode('utf8')

Or

javascritp_string_var = unicode(s, 'utf8')

Richard


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yebach

unread,
Jan 4, 2016, 11:59:00 AM1/4/16
to web...@googlegroups.com
Also

My query from db is workersDb = db(db.workers.w_organisation == org).select(db.workers.id, db.workers.w_nick_name).as_list()

where i already get data as [ 'Husi\xc4\x8d', 'Hoenigman', 'Sudac', .......]

if I say without .as_list() they are ok. Once I match them after another condition

for worker in workers:
                match = next((w for w in workersDb if int(w['id']) == int(worker)), None)
                if match:
                    matchedWorkers.append(match["w_nick_name"].decode('utf8'))

this decode has no effect at all

if I try with smth like unicode(s, 'utf8') as suggested my page is not loaded. no error but just not loaded. looks like javascript problem

Richard Vézina

unread,
Jan 4, 2016, 12:37:13 PM1/4/16
to web2py-users
From js directly? I not really good with js but I guess there is encoding too... From web2py you can't since everything use byte (str('byte string')) and not unicode (u'your properly encoded string')... So if you pass data from web2py what I showed you is the way of doing it...

Richard

On Mon, Jan 4, 2016 at 11:59 AM, Yebach <vid....@gmail.com> wrote:
Ok, but I would like to already see the right endocing when I get data from server database?

Yebach

unread,
Jan 20, 2016, 4:52:27 AM1/20/16
to web2py-users
In this case I get the list of string 

users = [u'Ga\u0161peri\u010d', u'GovednikK', u'Jam\u0161ek', u'Jurejev\u010di\u010d', u'Kati\u0107', u'Kostelec', u'Ko\u0161ele', u'Male\u0161i\u010d', u'Muhi\u010d', u'Re\u017eek', u'Sitar', u'Toma\u017ei\u010d', u'Viktorovski', u'Vuk\u0161ini\u010dD', u'\u010cerni\u010d', u'\u017defran'];
And there is a JS error Uncaught SyntaxError: Unexpected string

Richard Vézina

unread,
Jan 20, 2016, 9:41:13 AM1/20/16
to web2py-users
You need XML() web2py helper when you pass python string or generate js code from python you need it... 

In the view :

{{=XML(python_var_containing_text_or_js_code}}

Richard

Yebach

unread,
Jan 20, 2016, 9:44:05 AM1/20/16
to web2py-users
Reply all
Reply to author
Forward
0 new messages