Home > Back-end >  Converting a single variable of integers from json to a python list
Converting a single variable of integers from json to a python list

Time:01-16

Excuse my newbieness but I cannot find the solution.

I am using an API that returns my data in json, and after manipulation I need to turn it into a python list so I can do further calculation using each index as an integer from the list.

The json output is targeted to a variable and its output looks ok, but it is not as a list of the numbers.

Here is my code example:

import requests 
import json 
import time
import calendar
import pendulum
from mytoken import token


# Change serial
my_serial_numbers = ['123456789']

for serial in my_serial_numbers:
    try:
        recording_availibility_api = requests.get('https://api.sample.com/' serial '/path...',headers={"content-type": "application/json", "Authorization":"Bearer "   token})
        recordings_files = json.loads(recording_availibility_api.content.decode('utf-8'))
        for camId1 in (recordings_files['data'][0]['intervals']):
            cameraId1 = camId1.pop('end')
            dt_camera1 = pendulum.parse(cameraId1)
            time_camera1 = dt_camera1.int_timestamp
            time_camera1_str = str(time_camera1)
            print(time_camera1_str)
    except KeyError:
        print('edge is offline')
    continue

Output:

1641055957
1641104678
1641109926
1641142409
1641228862
1641237806
1641245124
1641306172
1641313527
1641315317
1641317850
1641320175
1641367757
1641383666

What I am trying to achieve: [ 1641055957, 1641104678, 1641109926, 1641142409,......]

I am attempting to have each of these outputs as an index in a list.

Much appreciated for any guidance.

CodePudding user response:

If you intended to have these an an index in a list, you have the problem that you would need a list of length ~1641383666 (just taken one of the outputs here for reference). A better way in Python is to use a dictionary, like so:

myDictionary = {}

Then, to assign:

myDictionary[1641383666] = None

and to access:

x = myDictionary[1641383666]

CodePudding user response:

If my understanding is correct, then you need to create a new list for the items read (I've named it time_strs in the code below) and .append your items to this list:

# imports not repeated

# Change serial
my_serial_numbers = ['123456789']

time_strs = []

for serial in my_serial_numbers:
    try:
        recording_availibility_api = requests.get('https://api.sample.com/' serial '/path...',headers={"content-type": "application/json", "Authorization":"Bearer "   token})
        recordings_files = json.loads(recording_availibility_api.content.decode('utf-8'))
        for camId1 in (recordings_files['data'][0]['intervals']):
            cameraId1 = camId1.pop('end')
            dt_camera1 = pendulum.parse(cameraId1)
            time_camera1 = dt_camera1.int_timestamp
            time_camera1_str = str(time_camera1)
            time_strs.append(time_camera1_str)
    except KeyError:
        print('edge is offline')
    continue

print(time_strs)
  •  Tags:  
  • Related