包含字符串的列表(https://i.stack.imgur.com/pOzik.png)
这是一个项目的字符串列表,我想从第6个字符及之后提取字典{}。
我应该将列表转换为字典吗?
data=
'[n {n "registration_number": "aj13870",n "status": "registreret",n "status_date": "2013-10-07t10:33:46.000+02:00",n "type": "personbil",n "use": "privat personk\u00f8rsel",n "first_registration": "2013-10-07+02:00",n "vin": "wdd2042021g129692",n "own_weight": null,n "cerb_weight": 1655,n "total_weight": 2195,n "axels": 2,n "pulling_axels": 1,n "seats": 5,n "coupling": false,n "trailer_maxweight_nobrakes": 750,n "trailer_maxweight_withbrakes": 1800,n "doors": 4,n "make": "mercedes-benz",n "model": "c-klasse",n "variant": "220 cdi blueefficiency t",n "model_type": "204 k",n "model_year": 2013,n "color": "gr\u00e5",n "chassis_type": "stationcar",n "engine_cylinders": 4,n "engine_volume": 2143,n "engine_power": 125,n "fuel_type": "diesel",n "registration_zipcode": "",n "vehicle_id": 9000000000384590,n "mot_info": {n "type": "periodisksyn",n "date": "2021-10-06",n "result": "godkendt",n "status": "aktiv",n "status_date": "2021-10-06",n "mileage": 106n },n "is_leasing": false,n "leasing_from": null,n "leasing_to": nulln }n]'
如果我试图找到键或值的索引,但它是一个字符串列表。我试图从字典中提取键和值,但它不起作用。
首先,您可以通过json.loads()
:
list
对象。import json
text = '[n {n "registration_number": "aj13870",n "status": "registreret",n "status_date": "2013-10-07t10:33:46.000+02:00",n "type": "personbil",n "use": "privat personk\u00f8rsel",n "first_registration": "2013-10-07+02:00",n "vin": "wdd2042021g129692",n "own_weight": null,n "cerb_weight": 1655,n "total_weight": 2195,n "axels": 2,n "pulling_axels": 1,n "seats": 5,n "coupling": false,n "trailer_maxweight_nobrakes": 750,n "trailer_maxweight_withbrakes": 1800,n "doors": 4,n "make": "mercedes-benz",n "model": "c-klasse",n "variant": "220 cdi blueefficiency t",n "model_type": "204 k",n "model_year": 2013,n "color": "gr\u00e5",n "chassis_type": "stationcar",n "engine_cylinders": 4,n "engine_volume": 2143,n "engine_power": 125,n "fuel_type": "diesel",n "registration_zipcode": "",n "vehicle_id": 9000000000384590,n "mot_info": {n "type": "periodisksyn",n "date": "2021-10-06",n "result": "godkendt",n "status": "aktiv",n "status_date": "2021-10-06",n "mileage": 106n },n "is_leasing": false,n "leasing_from": null,n "leasing_to": nulln }n]'
text_list = json.loads(text)
之后,你可以得到列表的第一个元素来实现你的字典:
print(text_list[0])
尝试将其作为JSON字符串读取。
json.loads('[n {n "registration_number": "aj13870",n "status": "registreret",n "status_date": "2013-10-07t10:33:46.000+02:00",n "type": "personbil",n "use": "privat personk\u00f8rsel",n "first_registration": "2013-10-07+02:00",n "vin": "wdd2042021g129692",n "own_weight": null,n "cerb_weight": 1655,n "total_weight": 2195,n "axels": 2,n "pulling_axels": 1,n "seats": 5,n "coupling": false,n "trailer_maxweight_nobrakes": 750,n "trailer_maxweight_withbrakes": 1800,n "doors": 4,n "make": "mercedes-benz",n "model": "c-klasse",n "variant": "220 cdi blueefficiency t",n "model_type": "204 k",n "model_year": 2013,n "color": "gr\u00e5",n "chassis_type": "stationcar",n "engine_cylinders": 4,n "engine_volume": 2143,n "engine_power": 125,n "fuel_type": "diesel",n "registration_zipcode": "",n "vehicle_id": 9000000000384590,n "mot_info": {n "type": "periodisksyn",n "date": "2021-10-06",n "result": "godkendt",n "status": "aktiv",n "status_date": "2021-10-06",n "mileage": 106n },n "is_leasing": false,n "leasing_from": null,n "leasing_to": nulln }n]')
更多信息请访问:https://www.w3schools.com/python/python_json.asp
请正确设置输入格式:
[
{
"registration_number": "aj13870",
"status": "registreret",
"status_date": "2013-10-07t10:33:46.000+02:00",
"type": "personbil",
"use": "privat personku00f8rsel",
"first_registration": "2013-10-07+02:00",
"vin": "wdd2042021g129692",
"own_weight": null,
"cerb_weight": 1655,
"total_weight": 2195,
"axels": 2,
"pulling_axels": 1,
"seats": 5,
"coupling": false,
"trailer_maxweight_nobrakes": 750,
"trailer_maxweight_withbrakes": 1800,
"doors": 4,
"make": "mercedes-benz",
"model": "c-klasse",
"variant": "220 cdi blueefficiency t",
"model_type": "204 k",
"model_year": 2013,
"color": "gru00e5",
"chassis_type": "stationcar",
"engine_cylinders": 4,
"engine_volume": 2143,
"engine_power": 125,
"fuel_type": "diesel",
"registration_zipcode": "",
"vehicle_id": 9000000000384590,
"mot_info": {
"type": "periodisksyn",
"date": "2021-10-06",
"result": "godkendt",
"status": "aktiv",
"status_date": "2021-10-06",
"mileage": 106
},
"is_leasing": false,
"leasing_from": null,
"leasing_to": null
}
]
还请提供什么不工作
如果你需要任何帮助与格式/如何询问/等等,这是指南。
要从这个字符串中提取键/值,你首先需要解析它:你可以使用JSON库。
然后可以使用内置函数获取键和项
import json
text = ...
data = json.loads(text)
data = data[0] # the data is wrapped in a list
print(data)
print(data.keys())
print(data.items())