У меня есть данные JSON, которые я хочу проанализировать для скорости и соответствующей метки времени, и я получил это далеко. Я обратил его сортировку, а также построил график, используя matplotlib. Теперь я хотел вывести Key с соответствующим значением, но я могу только напечатать ключ скорости, а не ключ метки времени.JSON Multiple Key Value Print
import json
import datetime
import pprint
from operator import itemgetter
import natsort
import matplotlib.pyplot as plt
import numpy as np
from collections import Counter
import pandas as pd
#path to gps data file in json format.
data_file = "waypoints.json"
def speed_ans(self, data_file):
pass
"""
def visualize_type(output):
#Visualize data by category in a bar graph
#This returns a dict where it sums the total per Category.
counter = Counter(item["Speed"] for item in output)
# Set the labels which are based on the keys of our counter.
labels = tuple("Speed")
# Set where the labels hit the x-axis
xlocations = np.arange(len(labels)) + 0.5
# Width of each bar
width = 0.5
# Assign data to a bar plot
plt.bar(xlocations, counter.values(), width=width)
# Assign labels and tick location to x- and y-axis
plt.xticks(xlocations + width/2, labels, rotation=90)
plt.yticks(range(0, max(counter.values()), 5))
# Give some more room so the labels aren't cut off in the graph
plt.subplots_adjust(bottom=0.4)
# Make the overall graph/figure larger
plt.rcParams['figure.figsize'] = 12, 8
# Save the graph!
plt.savefig("Graph.png")
plt.clf()
"""
if __name__ == '__main__':
with open(data_file) as f:
waypoints = json.load(f)
sorted_waypoints = natsort.natsorted(waypoints, key=itemgetter(*['Speed']), reverse = True)
#pprint.pprint(sorted_waypoints)
for e in sorted_waypoints:
for k, v in e.items():
if k == 'Speed' and v >= 6:
print k, v
waypoints.json Файл:
[{
"Latitude": 1.282143333,
"Timestamp": 1434368770,
"Speed": 7.696,
"Longitude": 103.850785
}, {
"Latitude": 1.282205,
"Timestamp": 1434368771,
"Speed": 7.233,
"Longitude": 103.850806667
}, {
"Latitude": 1.282205,
"Timestamp": 1434368772,
"Speed": 7.233,
"Longitude": 103.850806667
}, {
"Latitude": 1.282205,
"Timestamp": 1434368773,
"Speed": 7.444,
"Longitude": 103.850806667
}, {
"Latitude": 1.282261667,
"Timestamp": 1434368774,
"Speed": 6.933,
"Longitude": 103.850833333
}]
Спасибо, но это не то, что я хотел. В нем есть некоторые нежелательные вещи. – Dynamic
O/P, как это Speed 5.33 Timestamp 1434367770 скорость 5.222 Timestamp 1434367550 скорость 5.196 Timestamp 1434368742 Скорость 5,0 Отметки 1434367211 Отметки 1434367212 Отметки 1434367542 Отметки 1434367895 Отметки 1434368746 Timestamp 1434367378 Speed + Timestamp отлично, но после этого печатается длинная последовательность временных меток: | – Dynamic