gpt4 book ai didi

python - 如何从 (.db) 文件读取并为 python 变量赋值

转载 作者:行者123 更新时间:2023-12-04 08:40:31 25 4
gpt4 key购买 nike

我正在尝试从 (.db) 文件中提取 virtualIP 和 onvifPort。
我有一个 database.db 文件,它看起来像这样:


{
"devices": {
"193619374120978": {
"cameras": {
"14:a7:8b:08:e0:7c": {
"slots": {
"3": {
"onvifPort": "17005",
"rtspPort": "18005",
"virtualIP": "10.27.15.75",
"pubIP": "",
"pubOnvifPort": "0",
"pubRtspPort": "0"
}
}
},
"ac:cc:8e:a8:84:e1": {
"slots": {
"2": {
"onvifPort": "17003",
"rtspPort": "18003",
"virtualIP": "10.27.15.75",
"pubIP": "",
"pubOnvifPort": "0",
"pubRtspPort": "0"
}
}
},
"14:a7:8b:22:86:f6": {
"slots": {
"1": {
"onvifPort": "17004",
"rtspPort": "18004",
"virtualIP": "10.27.15.75",
"pubIP": "",
"pubOnvifPort": "0",
"pubRtspPort": "0"
}
}
}
}
}
}
}
我必须使用 python 读取这个文件并将这个值分配给 python 变量。
我是 python 的新手,我只是尝试过这样的事情,但我没有得到想要的输出。我想知道如何打开和读取 (.db) 文件并分配值



with open("bmdatabase.db","r") as infile:
inputs = json.load(infile)
#con = sqlite3.connect('bmdatabase.db')
#c = con.cursor()
for item in range(len(inputs["devices[].cameras[].slots[]"])):
IP = inputs["devices[].cameras[].slots[]"][item]["virtualIP"]
PORT = inputs["devices[].cameras[].slots[]"][item]["onvifPort"]

预期输出:
所有 3 台设备的用户名和密码都应保持不变


IP = virtualIP of slot[1]from database.db file
PORT = onvifPort of slot[1] from database.db file
USERNAME = "admin"
PASSWORD = "Abcd"

IP = virtualIP of slot[2]from database.db file
PORT = onvifPort of slot[2] from database.db file
USERNAME = "admin"
PASSWORD = "Abcd"

IP = virtualIP of slot[3]from database.db file
PORT = onvifPort of slot[3] from database.db file
USERNAME = "admin"
PASSWORD = "Abcd"

谁能帮我这个??

最佳答案

你可以试试这个例子来解析你的数据:

db = {
"devices": {
"193619374120978": {
"cameras": {
"14:a7:8b:08:e0:7c": {
"slots": {
"3": {
"onvifPort": "17005",
"rtspPort": "18005",
"virtualIP": "10.27.15.75",
"pubIP": "",
"pubOnvifPort": "0",
"pubRtspPort": "0"
}
}
},
"ac:cc:8e:a8:84:e1": {
"slots": {
"2": {
"onvifPort": "17003",
"rtspPort": "18003",
"virtualIP": "10.27.15.75",
"pubIP": "",
"pubOnvifPort": "0",
"pubRtspPort": "0"
}
}
},
"14:a7:8b:22:86:f6": {
"slots": {
"1": {
"onvifPort": "17004",
"rtspPort": "18004",
"virtualIP": "10.27.15.75",
"pubIP": "",
"pubOnvifPort": "0",
"pubRtspPort": "0"
}
}
}
}
}
}
}


parsed = []
for device_id in db['devices']:
for camera_mac in db['devices'][device_id]['cameras']:
for slot in db['devices'][device_id]['cameras'][camera_mac]['slots']:
parsed.append({
'IP': db['devices'][device_id]['cameras'][camera_mac]['slots'][slot]['virtualIP'],
'PORT': db['devices'][device_id]['cameras'][camera_mac]['slots'][slot]['onvifPort'],
'USERNAME': 'admin',
'PASSWORD': 'Abcd'
})


from pprint import pprint
pprint(parsed)
打印:
[{'IP': '10.27.15.75',
'PASSWORD': 'Abcd',
'PORT': '17005',
'USERNAME': 'admin'},
{'IP': '10.27.15.75',
'PASSWORD': 'Abcd',
'PORT': '17003',
'USERNAME': 'admin'},
{'IP': '10.27.15.75',
'PASSWORD': 'Abcd',
'PORT': '17004',
'USERNAME': 'admin'}]

编辑:从文件加载数据
import json
db = json.load(open('<your file-name>', 'r'))
print(db)

关于python - 如何从 (.db) 文件读取并为 python 变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64590568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com