Python错误:json.decoder.JSONDecodeError:预期值:json.loading有效json文件时的第2行第1列(char 2)

Python错误:json.decoder.JSONDecodeError:预期值:json.loading有效json文件时的第2行第1列(char 2),python,json,Python,Json,我试图将一个有效的json文件(根据jsonlint.com)加载到python中,然后将其索引到elasticsearch中 我的代码: file = open(json_file) i=1 json_data = [json.loads(line) for line in file] print(json_data) 我收到的错误信息: json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2) 我已经检

我试图将一个有效的json文件(根据jsonlint.com)加载到python中,然后将其索引到elasticsearch中

我的代码:

file = open(json_file)
i=1
json_data = [json.loads(line) for line in file]
print(json_data)
我收到的错误信息:

json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)
我已经检查了其他人的解决方案,这些解决方案的重点是json文件的无效性,但我的解决方案是完全有效的

我的JSON文件的缩短版本:

[
  {"sepalLength": 5.1, "sepalWidth": 3.5, "petalLength": 1.4, "petalWidth": 0.2, "species": "setosa"},
  {"sepalLength": 4.9, "sepalWidth": 3.0, "petalLength": 1.4, "petalWidth": 0.2, "species": "setosa"},
  {"sepalLength": 4.7, "sepalWidth": 3.2, "petalLength": 1.3, "petalWidth": 0.2, "species": "setosa"},
  {"sepalLength": 4.6, "sepalWidth": 3.1, "petalLength": 1.5, "petalWidth": 0.2, "species": "setosa"}
]

我不知道这里出现了什么问题。

您的列表理解试图加载与文件中的行数相同的对象,但只有一个文件。使用以下命令:

import json

with open(json_file, 'r') as f:
    json_data = [line for line in json.load(f)]
    print(json_data)

您的列表理解尝试加载文件中的行数相同的对象,但只有一个文件。使用以下命令:

import json

with open(json_file, 'r') as f:
    json_data = [line for line in json.load(f)]
    print(json_data)