Yijiao Liu
Yijiao Liu

Reputation: 304

How to show Chinese when print the result in python 2.7?

I got the results after running the code, which are

{u'cityCode': 53, u'addressComponent': {u'province': u'\u5409\u6797\u7701', 
u'city': u'\u957f\u6625\u5e02', u'direction': u'\u9644\u8fd1', 
u'street_number': u'1219\u53f7', u'district': u'\u671d\u9633\u533a', 
u'distance': u'16', u'country': u'\u4e2d\u56fd', u'adcode': u'220104', 
u'street': u'\u897f\u5b89\u80e1\u540c', u'country_code': 0}, u'business': 
u'\u897f\u5b89\u5927\u8def,\u5eb7\u5e73\u8857,\u6587\u5316\u5e7f\u573a', 
u'poiRegions': [], u'pois': [], u'location': {u'lat': 43.89833774287648, 
u'lng': 125.31364242699992}, u'sematic_description': 
u'\u5317\u836f\u5927\u53a6\u4e1c\u535784\u7c73', u'formatted_address':u'\u5409\u6797\u7701\u957f\u6625\u5e02\u671d\u9633\u533a\u897f\u5b89\u80e1\u540c1
219\u53f7'}

How can I see the Chinese in the terminal?

-------------update 1-----------

for k, v in mydict.iteritems(): print k, v

Results become:

cityCode 53
addressComponent {u'province': u'\u5409\u6797\u7701', u'city': 
u'\u957f\u6625\u5e02', u'direction': u'\u9644\u8fd1', u'street_number': 
u'1219\u53f7', u'district': u'\u671d\u9633\u533a', u'distance': u'16', 
u'country': u'\u4e2d\u56fd', u'adcode': u'220104', u'street': 
u'\u897f\u5b89\u80e1\u540c', u'country_code': 0}
business 西安大路,康平街,文化广场
poiRegions []
pois []
location {u'lat': 43.89833774287648, u'lng': 125.31364242699992}
sematic_description 北药大厦东南84米
formatted_address 吉林省长春市朝阳区西安胡同1219号

Still have some components not shown correctly

-------update 2 With the help of another question https://stackoverflow.com/a/3229493/7392051 I revised the code a little

def print_json(data):
if type(data) == dict:
        for k, v in data.iteritems():
            print_json(k)
            print_json(v)
else:
        print data

Now the results are cleaner

cityCode
53
addressComponent
province
吉林省
city
长春市
direction
附近
street_number
1219号
district
朝阳区
distance
16
country
中国
adcode
220104
street
西安胡同
country_code
0
business
西安大路,康平街,文化广场
poiRegions
[]
pois
[]
location
lat
43.8983377429
lng
125.313642427
sematic_description
北药大厦东南84米
formatted_address
吉林省长春市朝阳区西安胡同1219号

Upvotes: 0

Views: 409

Answers (1)

Philip Tzou
Philip Tzou

Reputation: 6458

You can approach this by utilizing the behaviour of ensure_ascii=False from json.dumps to encode the unicode characters nested in a structure into utf-8.

Python 2.7.13 (default, Jan  5 2017, 17:56:22)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = {'test': {'test': u'中文'}}
>>> print a
{'test': {'test': u'\u4e2d\u6587'}}
>>> import json
>>> print json.dumps(a)
{"test": {"test": "\u4e2d\u6587"}}
>>> print json.dumps(a, ensure_ascii=False)
{"test": {"test": "中文"}}

Upvotes: 2

Related Questions