t0m3k
t0m3k

Reputation: 317

How can I print a single item from this traccar output dictionary?

Goal: to change my output to only print latitude and longitude. I'd like to add the two strings as well but I can figure out how to do that. I'm struggling with this dictionary. Tried passing it to string via json but I keep getting errors.

Code:

import asyncio
import aiohttp
from pytraccar.api import API

HOST = "x"
PORT = x
USERNAME = "x"
PASSWORD = "x"

async def test():
    async with aiohttp.ClientSession() as session:
        data = API(LOOP, session, USERNAME, PASSWORD, HOST, PORT)
        await data.get_device_info()

        test55 = (data.positions)

        print(test55)


LOOP = asyncio.get_event_loop()
LOOP.run_until_complete(test())

Output:

[{'id': 55555, 'attributes': {'batteryLevel': 49.0, 'distance': 0.0, 'totalDistance': 866122.19, 'motion': False}, 'deviceId': 1, 'type': None, 'protocol': 'osmand', 'serverTime': '2020-06-19T15:25:58.160+0000', 'deviceTime': '2020-06-19T16:21:01.000+0000', 'fixTime': '2020-06-19T16:21:01.000+0000', 'outdated': False, 'valid': True, 'latitude': 39.204066, 'longitude': -71.677783, 'altitude': 41.93764706884086, 'speed': 0.0, 'course': 0.0, 'address': None, 'accuracy': 10.0, 'network': None}]

edit: I'm getting there:

pull1 = json.dumps(test55).split(',')


print(pull1[5])

output:

 "deviceId": 1

Now, maybe all I have to do is replace device ID.

edit: thank you all for the help. This is what I finally did:

pull = json.dumps(test55).split(',')
print(pull[10][12:]) #fix time
print(pull[13][12:]) #lat
print(pull[14][13:]) #long
print(pull[15][12:]) #alt
print(pull[19][12:]) #accuracy

Upvotes: 1

Views: 121

Answers (2)

t0m3k
t0m3k

Reputation: 317

answer:

pull = json.dumps(test55).split(',')
print(pull[10][12:]) #fix time
print(pull[13][12:]) #lat
print(pull[14][13:]) #long
print(pull[15][12:]) #alt
print(pull[19][12:]) #accuracy

I'll later pass this off as strings and send calls to Google sheets.

Upvotes: 1

Subhanshuja
Subhanshuja

Reputation: 418

Update

I think the code will be right this split-dictionary and rest of the code

if __name__ == "__main__": 
   dict = [{'id': 55555, 'attributes': {'batteryLevel': 49.0, 'distance': 0.0, 'totalDistance': 866122.19, 'motion': False}, 'deviceId': 1, 'type': None, 'protocol': 'osmand', 'serverTime': '2020-06-19T15:25:58.160+0000', 'deviceTime': '2020-06-19T16:21:01.000+0000', 'fixTime': '2020-06-19T16:21:01.000+0000', 'outdated': False, 'valid': True, 'latitude': 39.204066, 'longitude': -71.677783, 'altitude': 41.93764706884086, 'speed': 0.0, 'course': 0.0, 'address': None, 'accuracy': 10.0, 'network': None}] 
   print(dict)
   print(dict[0]['latitude'])
   print(dict[0]['longitude'])

Upvotes: 1

Related Questions