【问题标题】:using python to extract data with duplicate names from a json array使用python从json数组中提取具有重复名称的数据
【发布时间】:2021-03-15 21:09:21
【问题描述】:

如果我在这里使用了错误的术语,我将首先道歉,我是 python 的初学者。 我有一个包含 5 组数据的 json 数组。每个集合中的相应项目具有重复的名称。我可以在 java 中提取它们,但不能在 python 中提取它们。我想要的项目称为“summary_polyline”。在过去的几周里,我尝试了很多不同的方法,但到目前为止没有任何效果。 这是我的python的相关部分-

#!/usr/bin/env python3.6
import os
import sys
from dotenv import load_dotenv, find_dotenv

import polyline
import matplotlib.pyplot as plt
import json

with open ('/var/www/vk7krj/running/strava_activities.json', 'rt') as myfile:
    contents = myfile.read()

#print (contents)
#print (contents["summary_polyline"[1]])
activity1 = contents("summary_polyline"[1])

如果我取消注释“打印内容”,它会将文件打印到屏幕上。 我通过在线 json 格式检查器运行了 json,它通过了 如何提取五个“summay_polylines”并将它们分配给“activity1”到“activity5”?

【问题讨论】:

  • 感谢 James 编辑,非常感谢。

标签: python arrays json


【解决方案1】:

如果我没听错的话,你需要从文件中转换为红色的 json 文本数据。

with open ('/var/www/vk7krj/running/strava_activities.json', 'rt') as myfile:
    contents = myfile.read()
    
# json_contents is a List with dicts now
json_contents = json.loads(contents)
# list with activities
activities = []
for dict_item in json_contents:
    activities.append(dict_item)
# print all activities (whole file)
print(activities)
# print first activity
print(activities[0])
# print second activity
print(activities[1])

【讨论】:

  • 谢谢 Andrei,文件是 11k,可以放在这里吗?
  • 重读你的回答安德烈,我不想将数据放入 json 中,我需要从 json 数组中提取它。
  • 好的,我们来了解一下细节))你有没有这样的格式文件:{'t': ['a', 'b', 'c']}?
  • 几乎-它是这样开始的- [{"resource_state": 2, "athlete": {"id": 35005370, "resource_state": 1}, "name": "Bellerive parkrun", “距离”:5035.0,“移动时间”:1973,
  • Andrei,我刚刚在vk7krj.com/Ffiles.html 上放了一个指向文件的链接,下面的第二项是指向该文件的超链接。右键单击和下载应该可以解决问题。
猜你喜欢
  • 1970-01-01
  • 2014-03-07
  • 2021-07-15
  • 2015-03-28
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多