Python2.7打印结果时如何显示中文?

2024-09-27 19:09:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我在运行代码后得到的结果是

{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'}

我怎么能看到航站楼里的中国人?在

-----------更新1-----------

^{pr2}$

结果变成:

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号

仍然有一些部件没有正确显示

------更新2 在另一个问题的帮助下 https://stackoverflow.com/a/3229493/7392051 我修改了一下代码

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

现在结果更干净了

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号

Tags: streetcitydatacountryprintprovincecitycodeu6797
1条回答
网友
1楼 · 发布于 2024-09-27 19:09:27

您可以通过利用ensure_ascii=Falsefrom{}的行为将嵌套在结构中的unicode字符编码为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": "中文"}}

相关问题 更多 >

    热门问题