[Tutor] Retain UTF-8 Character in Python List

Peter Otten __peter__ at web.de
Mon Jun 1 11:55:56 CEST 2015


Boy Sandy Gladies Arriezona wrote:

> Hi, it's my first time in here. 

Welcome!

> I hope you don't mind if I straight to the
> question.
> I do some work in python 2 and my job is to collect some query and then
> send it to java program via json. We're doing batch update in Apache
> Phoenix, that's why I collect those query beforehand.
> 
> My question is:
> *Can we retain utf-8 character in list without changing its form into \xXX
> or \u00XX?* The reason is because that java program insert it directly "as
> is" without iterating the list. So, my query will be the same as we print
> the list directly.
> 
> Example:
> c = 'sffs © fafd'
> l = list()
> 
> l.append(c)
> 
> print l
> ['sffs \xc2\xa9 fafd']  # this will be inserted, not ['sffs © fafd']

>>> import json
>>> items = [u"sffs © fafd"] # unicode preferrable over str for non-ascii 
data
>>> print json.dumps(items, ensure_ascii=False)
["sffs © fafd"]




More information about the Tutor mailing list