gpt4 book ai didi

python - 使用 python 3.5 用希伯来语和英语解析 json 文件

转载 作者:行者123 更新时间:2023-12-05 07:52:02 24 4
gpt4 key购买 nike

我的 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)

谢谢!!

最佳答案

您只需要告诉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”!

关于python - 使用 python 3.5 用希伯来语和英语解析 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34124019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com