1

目标:将我的输出更改为仅打印纬度和经度。我也想添加这两个字符串,但我可以弄清楚如何做到这一点。我正在为这本词典苦苦挣扎。尝试通过 json 将其传递给字符串,但我不断收到错误消息。

代码:

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())

输出:

[{'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}]

编辑:我到了:

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


print(pull1[5])

输出:

 "deviceId": 1

现在,也许我所要做的就是替换设备 ID。

编辑:谢谢大家的帮助。这就是我最终所做的:

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
4

2 回答 2

1

更新

我认为代码将是正确的这个拆分字典和其余代码

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'])
于 2020-06-20T12:39:12.647 回答
1

回答:

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

稍后我会将其作为字符串传递,并将调用发送到 Google 表格。

于 2020-06-20T13:17:57.890 回答