python - 用数据框替换 json 文件中的数据
问题描述
我有以下格式的 json 文件
"rawDataBody":
{
"data": [
{
"name": "Test",
"unit": "",
"format": "integer",
"key": "Test"
}
],
"dataBlock": [
[7, 1730569828, 3490, 1608636960, 30.62, 1003.82, 44.14, 683806.38, 2, 1, 0, 0],
[0, 1730563432, 3545, 1608636960, 29.89, 1003.52, 39.25, 557582.38, 2, 1, 0, 0],
[1, 1730579048, 3571, 1608636960, 29.79, 1003.45, 41.07, 494566.53, 2, 1, 0, 0],
[2, 1730568292, 3595, 1608636960, 29.62, 1003.40, 42.72, 546424.75, 2, 1, 0, 0],
我想用我的数据框替换 dataBlock 中的数据并将整个提取到 json 文件中
5.0,1730566755.0,22.11,1608636969.0,33.42,1003.5,39.78,60591.71,9.0,1.0,0.0,0.0
6.0,1730551139.0,22.14,1608636969.0,33.27,1003.64,38.77,77906.27,9.0,1.0,0.0,0.0
6.0,1730551139.0,22.14,1608636969.0,33.27,1003.64,38.77,77906.27,9.0,1.0,0.0,0.0
5.0,1730566755.0,42.11,1608636969.0,33.42,1003.5,39.78,60591.71,9.0,1.0,0.0,0.0
我试过的代码是。我不知道为什么这不起作用。有人可以帮我吗
with open('data.txt', 'r') as file:
jsonData = json.load(file)
dfFinalResult = dfFinalResult.values.tolist()
for item in jsonData['rawDataBody']['dataBlock']:
item = dfFinalResult
with open('newjsonfile.txt', 'w') as file:
json.dump(jsonData, file)
解决方案
你似乎在说我的建议奏效了。
这是我认为你应该拥有的:
with open('data.txt', 'r') as file:
jsonData = json.load(file)
dfFinalResult = dfFinalResult.values.tolist()
jsonData['rawDataBody']['dataBlock'] = dfFinalResult
with open('newjsonfile.txt', 'w') as file:
json.dump(jsonData, file)
推荐阅读
- java - 如何在 Mockobject 的帮助下检查真实对象的空值?
- node.js - 在 Google App Engine 上部署后 Node Server 不会自动重启
- django - 如何向 Django Oscar Order 模型添加字段?
- azure - 跨订阅移动 Azure SQL 托管实例
- jenkins - 为 Jenkins 多分支管道配置 JIRA 触发器插件
- nsis - NSIS,不显示 uac 对话框
- ruby-on-rails - Asset compilation takes too long in a rails production server. Is it possible to reduce by utilizing local?
- django - 如何在 django 中间件中执行内容协商?
- python - 有没有办法获取与 json 文件值中的字符串匹配的所有键并将它们输出到文本文件?
- vue.js - 如何防止 v-text-field 上的 TypeError 输入键