如何打印此traccar输出字典中的单个项目?

2024-09-27 02:24:12 发布

您现在位置:Python中文网/ 问答频道 /正文

目标:将输出更改为仅打印纬度和经度。我也想添加这两个字符串,但我知道怎么做。我正在努力读这本词典。尝试通过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

Tags: 字符串importnoneloopapiasynciojsonhost
2条回答

答复:

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 sheets发送调用

更新

我认为代码在这个split-dictionary和代码的其余部分都是正确的

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'])

相关问题 更多 >

    热门问题