【发布时间】:2018-04-04 23:08:23
【问题描述】:
在 Google 上到处查看,但没有成功找到解决此问题的方法,我继续收到以下错误:
JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 2)
错误发生在我的 Python 文件中的 row = json.loads(row) 行。 JSON 文件包含来自 2015-05 的 Reddit cmets 的一部分:
JSON (learn\learning_data\2015\RC_2015-05):
{
"created_utc": "1430438400",
"ups": 4,
"subreddit_id": "t5_378oi",
"link_id": "t3_34di91",
"name": "t1_cqug90g",
"score_hidden": false,
"author_flair_css_class": null,
"author_flair_text": null,
"subreddit": "soccer_jp",
"id": "cqug90g",
"removal_reason": null,
"gilded": 0,
"downs": 0,
"archived": false,
"author": "rx109",
"score": 4,
"retrieved_on": 1432703079,
"body": "\u304f\u305d\n\u8aad\u307f\u305f\u3044\u304c\u8cb7\u3063\u305f\u3089\u8ca0\u3051\u306a\u6c17\u304c\u3059\u308b\n\u56f3\u66f8\u9928\u306b\u51fa\u306d\u30fc\u304b\u306a",
"distinguished": null,
"edited": false,
"controversiality": 0,
"parent_id": "t3_34di91"
}
*JSON 数据只是我实际拥有的一小部分,我无法更改格式。 例如。
{
"text": "data",
"text": "data"
}
{
"text2": "data",
"text2": "data"
}
{
"text3": "data",
"text3": "data"
}
etc...
Python (learn\main.py):
with open("learning_data/{}/RC_{}".format(timeframe.split('-')[0], timeframe), buffering=1000) as f:
for row in f:
row_counter += 1
row = json.loads(row)
body = format_data(row['body'])
created_utc = row['created_utc']
parent_id = row['parent_id']
comment_id = row['name']
score = row['score']
subreddit = row['subreddit']
parent_data = find_parent(parent_id)
if score >= 2:
if acceptable(body):
existing_comment_score = find_existing_score(parent_id)
JSON 文件已经在需要双引号的所有内容上添加了双引号。 如果有其他错误导致这个 JSONLint.com 声称 JSON 没有它们。
我一直在从this tutorial 引用我的代码,其中教程的代码运行良好,没有任何错误(这是根据附加的视频,使用上面链接中的代码,我仍然得到错误)。因为教程使用的是 Python 3.5,所以我降级了我的 Python 版本并继续得到同样的错误。
如果 JSON 已经使用双引号并且由 JSONLint 有效,那么导致此错误的原因是什么?
【问题讨论】:
-
您不必为每个 JSON 行调用
json.loads。您需要传递完整的 JSON 作为参数,例如,json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') -
我拥有的 JSON 数据无法更改,我添加了一个示例,说明它目前的样子。