1

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
t0m3k
  • 307
  • 1
  • 14
  • your code is only to login, no function is inserted of lat and long into google sheet, please insert last your code – Subhanshuja Jun 20 '20 at 12:34
  • 1
    I don't need help inserting anything into Google sheets. I need to know how to change my output from what it is to only latitude and longitude so I can later pass that to Google sheets. – t0m3k Jun 20 '20 at 12:36
  • oh okey, you need split your data.positions into array, i will update the answer with the simple code – Subhanshuja Jun 20 '20 at 12:40
  • 1
    The output is giving me a headache. so many different brackets and commas. I'll try.. – t0m3k Jun 20 '20 at 12:43

2 Answers2

1

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'])
Subhanshuja
  • 390
  • 1
  • 3
  • 20
1

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.

t0m3k
  • 307
  • 1
  • 14