python - 读取 .json 的第二个条目
问题描述
我正在尝试读取 .json 文件中数组列表的密度条目。他从一开始就是文件的一小部分:
["time_tag","density","speed","temperature"],["2019-04-14 18:20:00.000","4.88","317.9","11495"],["2019-04-14 18:21:00.000","4.89","318.4","11111"]
这是我到目前为止的代码:
with open('plasma.json', 'r') as myfile:
data = myfile.read()
obj = json.loads(data)
print(str(obj['density']))
它应该打印密度列下的所有内容,但我收到一条错误消息,提示无法打开文件
解决方案
您确定您的数据是有效的 json 而不是 csv 吗?
由于上面提供的数据片段与 csv 文件而不是 json 相匹配。
您将能够通过以下方式读取density
csv 的密钥:
import csv
input_file = csv.DictReader(open("plasma.csv"))
for row in input_file:
print(row['density'])
数据格式为 csv
["time_tag","density","speed","temperature"]
["2019-04-14 18:20:00.000","4.88","317.9","11495"]
["2019-04-14 18:21:00.000","4.89","318.4","11111"]
结果
4.88
4.89
推荐阅读
- mysql - 如何将 1D mysql 表“展平”或“折叠”成 2D?
- javascript - 在选择框中填充 optgroup 上的选项,选项数据相互覆盖
- ios - Objective-C 生成的接口头和 CocoaPods
- sql - 执行 SQL 任务中的存储过程不持久化对数据库的更改
- xcode - 无法将 Xcode 服务器从 Xcode 9.4.1 更新到 Xcode 10
- arrays - 如何从firestore检索数组的特定值
- java - 为什么以前的 Activity 在设置 Activity 工具栏上按向上按钮被破坏?
- python - Python布尔检查其他功能
- mysql - SQL从表中添加一个额外的列
- angular - 如何访问可观察列表中的对象