python - 从 json 文件中提取时出现 Python 和 JSON 错误
问题描述
有谁知道为什么这个 python/json 代码没有打印出来,而是在解释器中给我这个错误?我查看了 json 脚本,没有发现任何似乎有问题的地方。我最初的原因是创建相同的 json 和 yaml 文件并在 Python 中提取/打印数据。
Traceback (most recent call last):
File "C:/Users/<REMOVED>/PycharmProjects/test_project/yaml and json extract.py", line 5, in <module>
python_dict = json.load(json_file)
File "C:\Users\<REMOVED>\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 293, in load
return loads(fp.read(),
File "C:\Users\<REMOVED>\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\<REMOVED>\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\<REMOVED>\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 3 column 11 (char 23)
{ "cars": [
"ford": [
{"model": "focus",
"price": 12000,
"transmission": "manual",
"colours": ["red", "blue", "green", "white"]}
]
"kia": [
{
"model": "shuma",
"price": 8000,
"transmission": "manual",
"colours": ["red", "blue", "green", "white"]}
]
"honda": [
{
"model": "civic",
"price": 15000,
"transmission": "automatic",
"colours": ["red", "blue", "green", "white"]}
]
]
}
这是python代码:
import json
import yaml
with open("C:/Users/<REMOVED>/Desktop/test/cars.json", "r") as json_file:
python_dict = json.load(json_file)
json_indent = json.dumps(python_dict, indent=4)
print(json_indent)
解决方案
您的 json 文件格式错误。以下版本至少是一个有效的 json:
{
"cars": [{
"ford": [{
"model": "focus",
"price": 12000,
"transmission": "manual",
"colours": ["red", "blue", "green", "white"]
}],
"kia": [{
"model": "shuma",
"price": 8000,
"transmission": "manual",
"colours": ["red", "blue", "green", "white"]
}],
"honda": [{
"model": "civic",
"price": 15000,
"transmission": "automatic",
"colours": ["red", "blue", "green", "white"]
}]
}]
}
推荐阅读
- c# - 收到错误提示“转换 varchar 值时转换失败”
- sql - 用于将成本摊销到以下月份的 SQL
- javascript - jQuery重置newsticker的先前效果而不是通过单击替换为新效果
- react-native - 是否可以在同一行中分隔组件?[反应原生设计]
- node.js - MikroORM Eager 设置为 false 但仍在加载关系
- python - 如何在api calll python中传递变量?
- r - 为多个嵌套组传播控件
- r - rhandsontable 新数据添加:seq.default 中的错误:参数“length.out”的长度必须为 1
- javascript - 有没有办法从单个 URL 获取所有文件链接?
- mongodb - 使用 MongoDB 时的 Graphql 接口