【问题标题】:I can't convert a JSON format file into a python format file [closed]我无法将 JSON 格式文件转换为 python 格式文件 [关闭]
【发布时间】:2017-07-06 11:53:41
【问题描述】:

我在做pythonAPI练习,这是我完成的API地址:

http://api.juheapi.com/japi/toh?key=10245c2b75fa03d527204967ef7291cb&v=1.0&month=07&day=05

如果您使用浏览器打开,会显示正确的 JSON 文件。但我无法正确 结果使用下面的codecode。

page = urllib.urlopen(url)
html = page.read() 
dic_json = json.loads(html)
print dic_json

【问题讨论】:

  • 你遇到了什么错误
  • 对不起,请删除地址]
  • 像这样:{u'reason': u'\u8bf7\u6c42\u6210\u529f\uff01', u'error_code': 0, u'result': [{u'des': u'\u5728602\u5e74\u524d\u7684\u4eca\u5929\uff0c1415\u5e747\u67086\u65e5 (\u519c\u5386\u516d\u6708\u521d\u4e00)\uff0c\u637\4\u514b\ufdu8\u514b\u6767 \u4e49\u8005\u80e1\u53f8\u9047\u96be\u3002',
  • 这有什么问题??这是python中的dict类型。我想你是说 print 语句没有显示非英文字母,而是显示这个 \u8bf7 类型值?
  • @Swagat 我认为你的意思是“非 ASCII 字符”。

标签: python json


【解决方案1】:

json 中没有任何错误。它成功地将json解析为python dict。你应该看看 python unicode。当您尝试在 python 2 中打印 unicode 时,您会发现这样,但在浏览器中打开时,它将是正确的非 ASCII 字符。

Python2 将非 ASCII 字符存储成这样的格式。

要阅读这个特定的 dict ,你可以这样做。

for x,y in dic_json.items():
    print x,":", y

【讨论】:

  • 哦,好的,那么,我如何在控制台中打印 Unicode?​​span>
  • 试试 print dic_json['reason'] ,例如
猜你喜欢
  • 1970-01-01
  • 2021-09-11
  • 2021-10-15
  • 1970-01-01
  • 2015-12-17
  • 2018-03-06
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多