【问题标题】:parsing json file with hebrew and english with python 3.5用 python 3.5 用希伯来语和英语解析 json 文件
【发布时间】:2016-03-11 11:48:25
【问题描述】:

我的 json 文件,嗯,它的一部分看起来像:

[
  {
    "id": 472,
    "name": "אבו גוש",
    "engName": "ABU GHOSH"
  },
  {
    "id": 473,
    "name": "אבו סנאן",
"engName": "ABU SINAN"
  },
  {
     "id": 1342,
    "name": "אבו קורינאת (יישוב)",
    "engName": "ABU QUREINAT"
  },
]

等等。

我的部分代码看起来像:

with open('israelCities.json') as data_file:
    jsonData = json.loads(data_file.read().encode('utf8'))
    print(jsonData)

它在第二行失败(jsonData = ....), 我是 python 新手,没有看到任何类似的问题, 任何帮助将不胜感激

谢谢!!

编辑

这两个对我来说很完美:

 import json
 import urllib.request
 url='https://raw.githubusercontent.com/royts/israel-cities/master/israel-cities.json'
 data = urllib.request.urlopen(url).read().decode('utf-8')
 json.loads(data)

还有这个:

import json
import requests

r = requests.get('https://raw.githubusercontent.com/royts/israel-cities/master/israel-cities.json')
with open('israelCities.json', 'w') as f:
    json.dump(r.json(), f)


with open('israelCities.json') as f:
json_data = json.load(f)

谢谢!!

【问题讨论】:

  • 失败是什么意思?你得到了什么错误?
  • 你好,不知道为什么,我的原帖被删了,pycharm没有错误

标签: python json parsing hebrew


【解决方案1】:

这来自您的代码:json.loads(data_file.read().encode('utf8')) 尝试从文件中读取数据,然后将其转换为 utf8。

试试这个:json.loads(data_file.read(), encoding='utf8'),意思是:读这个,写成utf8。

当然,文件应该保存为utf-8,否则将无法使用。


编辑:

按照@mhawke 的建议,简化用法并使用 OP 的原始文件,这样可以:

>>> httpresponse = urllib.urlopen('https://raw.githubusercontent.com/royts/israel-cities/master/israel-cities.json')
>>> json.load(httpresponse)

编辑 2:

如果您使用的是 Python 3,请尝试以下方法:

>>> import json
>>> import urllib.request
>>> url='https://raw.githubusercontent.com/royts/israel-cities/master/israel-cities.json'
>>> data = urllib.request.urlopen(url).read().decode('utf-8')
>>> json.loads(data)

【讨论】:

  • 真的不知道它是如何工作的,它在 urlopen 上失败了......只是把我踢到了 if name == "main": main() 部分...
  • 您可能正在使用 Python 3。我添加了一个 Python 3 示例,它也适用于我。
  • 嗨,你的新编辑工作了!谢谢!,有没有机会让它成为本地人?
【解决方案2】:

您只需要告诉loads 编码是什么,而不是尝试将其转换为编码。

所以,

import json

with open('israelCities.json') as data_file:
    jsonData = json.loads(data_file.read(), encoding='utf-8')
    print(jsonData)

将产生

[{u'engName': u'ABU GHOSH', u'id': 472, u'name': u'\u05d0\u05d1\u05d5\u05d2\u05d5\u05e9'}, {u'engName' : u'ABU SINAN', u'id': 473, u'name': u'\u05d0\u05d1\u05d5 \u05e1\u05e0\u05d0\u05df'}, {u'engName': u'ABU QUREINAT', u'id': 1342, u'name': u'\u05d0\u05d1\u05d5\u05e7\u05d5\u05e8\u05d9\u05e0\u05d0\u05ea (\u05d9\u05d9\u05e9\u05d5\u05d1)'}]

但前提是您首先在编码中将 israelCities.json 保存为“utf-8”!

【讨论】:

  • 感谢您的回复,不知道为什么,但即使使用您的代码也无法正常工作: with open('israelCities.json') as data_file: jsonData = json.loads(data_file .read(), encoding='utf8') print(jsonData) 从这里下载的json文件:github.com/royts/israel-cities
  • 您需要将该文件显式保存为在utf-8 中编码。你有visublime 什么的吗?
  • 我确实用 np++ 将它保存为 utf8,仍然没有运气
  • 嗯。这很奇怪。我将仅该代码的输出粘贴到 py2.7 中,并将该文件保存为 utf-8。那么当你运行你得到什么输出?
  • with open('JsonProperits2.json') as data_file: data_file = <_io.textiowrapper name="JsonProperits2.json" mode="r" encoding="cp1255"> 然后上线:jsonData = json.loads(data_file.read(), encoding='utf-8') 它被踢到 if name == "main": ,没有任何输出,在使用 pyChram 调试模式,非常感谢您的尝试!
【解决方案3】:

您无需在文件上调用read()。请改用json.load()

import json

with open('israelCities.json') as data_file:
    jsonData = json.load(data_file)

如果文件是 UTF8 编码的(并且 git repo israel-cities 中的那个是),则不需要将编码指定为 json.load()


更新

从其他答案中的 cmets 看来,您可能正在从 github 下载文件并保存它。如果您克隆存储库,则该文件应该没有问题 - 它已经是 UTF8 编码的。如果您不确定是否可以使用 requests 库下载文件并将其显式保存为 json:

import json
import requests

r = requests.get('https://raw.githubusercontent.com/royts/israel-cities/master/israel-cities.json')
with open('israelCities.json', 'w') as f:
    json.dump(r.json(), f)

现在你肯定有一个可以加载的文件:

with open('israelCities.json') as f:
    json_data = json.load(f)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 2015-09-21
    • 1970-01-01
    • 2016-05-03
    • 2018-01-08
    • 2019-07-25
    • 2013-08-07
    相关资源
    最近更新 更多