【问题标题】:JSON Multiple Key Value PrintJSON 多键值打印
【发布时间】:2016-02-11 08:24:24
【问题描述】:

我有一些 JSON 数据,我想分析它们的速度和相应的时间戳,我已经做到了这一点。我已经对其进行了反向排序,并使用 matplotlib 绘制了一个图表。现在我想输出具有相应值的键,但我只能打印速度键而不是时间戳键。

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
}]

【问题讨论】:

    标签: python json data-analysis


    【解决方案1】:

    不确定是否有帮助。

    for e in sorted_waypoints:
        for k, v in e.items():
            if (k == 'Speed' or k == 'Timestamp') and v >= 5:
            print k, v
    

    【讨论】:

    • 谢谢它的工作,但输出不是我想要的。它在输出中有一些不需要的东西。
    • The O/P is like this Speed 5.33 Timestamp 1434367770 Speed 5.222 Timestamp 1434367550 Speed 5.196 Timestamp 1434368742 Speed 5.0 Timestamp 1434367211 Timestamp 1434367212 Timestamp 1434367542 Timestamp 1434367895 Timestamp 1434368746 Timestamp 1434367378 The Speed + Timestamp combo is fine but after正在打印一长串时间戳:|
    【解决方案2】:

    您只打印速度键的原因是您在打印之前检查键值是否为“速度”。您所要做的就是将条件修改为您要打印的内容。

    【讨论】:

    • 我之前尝试过这样做但没有成功,我在前一位评论者的帮助下解决了这个问题。无论如何也感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多