#python #arrays #json #graph
#python #массивы #json #График
Вопрос:
Данные JSON Graph, которые я пытаюсь использовать, не содержат заданных ключей. Он возвращает данные за последние 180 дней. Первые числа — это миллисекунды, прошедшие с момента создания epoch, значения — это цена в игре. Мне нужно знать, как анализировать эти данные, чтобы я мог позже назначать переменные при использовании функции для преобразования первых значений в даты из миллисекунд:
def convert_from_ms( milliseconds ):
seconds, milliseconds = divmod(milliseconds,1000)
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
seconds = seconds milliseconds/1000
return days, hours, minutes, seconds
А затем библиотека построения графиков (совет по этому поводу также приветствовался бы, скорее всего, просто используйте matplot)
Данные выглядят следующим образом:
{"daily":{"1582416000000":193,"1582502400000":193,"1582588800000":190,"1582675200000":189,"1582761600000":189,"1582848000000":191,"1582934400000":191,"1583020800000":190,"1583107200000":190,"1583193600000":191,"1583280000000":192,"1583366400000":191,"1583452800000":191,"1583539200000":189,"1583625600000":186,"1583712000000":182,"1583798400000":177,"1583884800000":173,"1583971200000":170,"1584057600000":167,"1584144000000":165,"1584230400000":170,"1584316800000":176,"1584403200000":179,"1584489600000":179,"1584576000000":182,"1584662400000":187,"1584748800000":187,"1584835200000":185,"1584921600000":183,"1585008000000":185,"1585094400000":185,"1585180800000":183,"1585267200000":183,"1585353600000":185,"1585440000000":185,"1585526400000":184,"1585612800000":186,"1585699200000":189,"1585785600000":190,"1585872000000":192,"1585958400000":193,"1586044800000":192,"1586131200000":192,"1586217600000":192,"1586304000000":193,"1586390400000":195,"1586476800000":196,"1586563200000":196,"1586649600000":191,"1586736000000":182,"1586822400000":174,"1586908800000":168,"1586995200000":171,"1587081600000":176,"1587168000000":179,"1587254400000":180,"1587340800000":174,"1587427200000":167,"1587513600000":168,"1587600000000":172,"1587686400000":180,"1587772800000":185,"1587859200000":186,"1587945600000":184,"1588032000000":189,"1588118400000":196,"1588204800000":200,"1588291200000":204,"1588377600000":207,"1588464000000":209,"1588550400000":207,"1588636800000":207,"1588723200000":207,"1588809600000":207,"1588896000000":206,"1588982400000":200,"1589068800000":191,"1589155200000":183,"1589241600000":184,"1589328000000":190,"1589414400000":194,"1589500800000":198,"1589587200000":197,"1589673600000":192,"1589760000000":184,"1589846400000":182,"1589932800000":186,"1590019200000":186,"1590105600000":188,"1590192000000":186,"1590278400000":184,"1590364800000":181,"1590451200000":177,"1590537600000":172,"1590624000000":174,"1590710400000":180,"1590796800000":177,"1590883200000":174,"1590969600000":172,"1591056000000":173,"1591142400000":171,"1591228800000":172,"1591315200000":174,"1591401600000":170,"1591488000000":170,"1591574400000":172,"1591660800000":173,"1591747200000":174,"1591833600000":175,"1591920000000":176,"1592006400000":175,"1592092800000":174,"1592179200000":171,"1592265600000":170,"1592352000000":169,"1592438400000":167,"1592524800000":165,"1592611200000":162,"1592697600000":159,"1592784000000":157,"1592870400000":162,"1592956800000":167,"1593043200000":168,"1593129600000":167,"1593216000000":162,"1593302400000":159,"1593388800000":157,"1593475200000":159,"1593561600000":165,"1593648000000":170,"1593734400000":172,"1593820800000":172,"1593907200000":173,"1593993600000":174,"1594080000000":176,"1594166400000":178,"1594252800000":182,"1594339200000":182,"1594425600000":177,"1594512000000":175,"1594598400000":177,"1594684800000":176,"1594771200000":179,"1594857600000":181,"1594944000000":180,"1595030400000":177,"1595116800000":175,"1595203200000":174,"1595289600000":176,"1595376000000":175,"1595462400000":171,"1595548800000":170,"1595635200000":169,"1595721600000":169,"1595808000000":170,"1595894400000":172,"1595980800000":171,"1596067200000":171,"1596153600000":170,"1596240000000":170,"1596326400000":172,"1596412800000":174,"1596499200000":175,"1596585600000":176,"1596672000000":178,"1596758400000":180,"1596844800000":180,"1596931200000":179,"1597017600000":178,"1597104000000":174,"1597190400000":170,"1597276800000":165,"1597363200000":160,"1597449600000":162,"1597536000000":165,"1597622400000":165,"1597708800000":166,"1597795200000":164,"1597881600000":163},"average":{"1582416000000":201,"1582502400000":200,"1582588800000":198,"1582675200000":196,"1582761600000":195,"1582848000000":193,"1582934400000":191,"1583020800000":190,"1583107200000":189,"1583193600000":189,"1583280000000":188,"1583366400000":189,"1583452800000":189,"1583539200000":189,"1583625600000":189,"1583712000000":189,"1583798400000":189,"1583884800000":189,"1583971200000":188,"1584057600000":187,"1584144000000":186,"1584230400000":186,"1584316800000":185,"1584403200000":185,"1584489600000":185,"1584576000000":184,"1584662400000":184,"1584748800000":184,"1584835200000":184,"1584921600000":183,"1585008000000":183,"1585094400000":183,"1585180800000":183,"1585267200000":182,"1585353600000":182,"1585440000000":182,"1585526400000":182,"1585612800000":182,"1585699200000":182,"1585785600000":182,"1585872000000":182,"1585958400000":182,"1586044800000":182,"1586131200000":182,"1586217600000":182,"1586304000000":182,"1586390400000":183,"1586476800000":184,"1586563200000":185,"1586649600000":186,"1586736000000":186,"1586822400000":186,"1586908800000":186,"1586995200000":186,"1587081600000":186,"1587168000000":185,"1587254400000":185,"1587340800000":185,"1587427200000":184,"1587513600000":184,"1587600000000":183,"1587686400000":183,"1587772800000":183,"1587859200000":183,"1587945600000":183,"1588032000000":183,"1588118400000":184,"1588204800000":184,"1588291200000":185,"1588377600000":185,"1588464000000":186,"1588550400000":186,"1588636800000":187,"1588723200000":187,"1588809600000":188,"1588896000000":188,"1588982400000":188,"1589068800000":188,"1589155200000":188,"1589241600000":188,"1589328000000":188,"1589414400000":189,"1589500800000":190,"1589587200000":190,"1589673600000":191,"1589760000000":191,"1589846400000":191,"1589932800000":192,"1590019200000":192,"1590105600000":193,"1590192000000":193,"1590278400000":193,"1590364800000":193,"1590451200000":193,"1590537600000":193,"1590624000000":192,"1590710400000":192,"1590796800000":191,"1590883200000":190,"1590969600000":189,"1591056000000":187,"1591142400000":186,"1591228800000":185,"1591315200000":184,"1591401600000":183,"1591488000000":182,"1591574400000":181,"1591660800000":180,"1591747200000":180,"1591833600000":179,"1591920000000":179,"1592006400000":178,"1592092800000":178,"1592179200000":177,"1592265600000":176,"1592352000000":175,"1592438400000":175,"1592524800000":174,"1592611200000":173,"1592697600000":172,"1592784000000":172,"1592870400000":171,"1592956800000":170,"1593043200000":170,"1593129600000":170,"1593216000000":169,"1593302400000":169,"1593388800000":168,"1593475200000":168,"1593561600000":167,"1593648000000":167,"1593734400000":167,"1593820800000":167,"1593907200000":167,"1593993600000":167,"1594080000000":168,"1594166400000":168,"1594252800000":168,"1594339200000":168,"1594425600000":168,"1594512000000":168,"1594598400000":168,"1594684800000":169,"1594771200000":169,"1594857600000":169,"1594944000000":170,"1595030400000":170,"1595116800000":170,"1595203200000":171,"1595289600000":171,"1595376000000":172,"1595462400000":172,"1595548800000":172,"1595635200000":172,"1595721600000":172,"1595808000000":173,"1595894400000":173,"1595980800000":173,"1596067200000":174,"1596153600000":174,"1596240000000":174,"1596326400000":174,"1596412800000":174,"1596499200000":174,"1596585600000":174,"1596672000000":174,"1596758400000":174,"1596844800000":174,"1596931200000":174,"1597017600000":174,"1597104000000":174,"1597190400000":174,"1597276800000":174,"1597363200000":173,"1597449600000":172,"1597536000000":172,"1597622400000":171,"1597708800000":171,"1597795200000":171,"1597881600000":170}}```
Я перепробовал много вариантов, похожих на этот:
data = json.loads(graph_get.text)
raw_graph_data = {}
graph_dates = {}
graph_prices = {}
if('daily' in data):
for key in data['daily']:
raw_graph_data[key] = data['daily'][key]
for x in raw_graph_data:
isolate_dates = x.replace("'", '')
info = isolate_dates.split("': ")
graph_dates[x] = info[0]
graph_prices[x] = info[1]
Но я знаю, что это неправильный способ сделать это.. Я знаю, что не могу использовать x для записи в массив. Я просто в тупике. Я раньше не работал с «динамическими» данными JSON, подобными этому. Приветствуются любые указания.
Ответ №1:
Я не уверен на 100%, чего вы пытаетесь достичь, но вы можете использовать следующий код для обработки и построения графика данных. Вместо использования пользовательского метода для преобразования миллисекунд в даты я использовал модуль datetime.
from datetime import datetime
import matplotlib.pyplot as plt
dates = []
prices = []
for key, value in data.get('daily', {}).items():
dates.append(datetime.fromtimestamp(int(key) / 1000))
prices.append(value)
plt.plot(dates, prices)
plt.show()