Home > Net >  how can i convert a list of letters into a dictionary
how can i convert a list of letters into a dictionary

Time:11-27

List that contains strings (https://i.stack.imgur.com/pOzik.png)

This is a string list of items and I want to extract the dictionary {} that is from the 6th character and after.

Should I convert list to dictionary?

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": 106\n        },\n        "is_leasing": false,\n        "leasing_from": null,\n        "leasing_to": null\n    }\n]'

If I try to find the index of the keys or values but it is a list of strings. I tried to extract the keys and values from the dictionary but it doesn't work.

CodePudding user response:

First, you can make you data a python list object via json.loads():

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": 106\n        },\n        "is_leasing": false,\n        "leasing_from": null,\n        "leasing_to": null\n    }\n]'
text_list = json.loads(text)

After that, you can get first element of your list to achieve your dictionary:

print(text_list[0])

CodePudding user response:

Try reading it as a JSON string.


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": 106\n        },\n        "is_leasing": false,\n        "leasing_from": null,\n        "leasing_to": null\n    }\n]')

For more info read: https://www.w3schools.com/python/python_json.asp

CodePudding user response:

Please format your input correctly:

[
    {
        "registration_number": "aj13870",
        "status": "registreret",
        "status_date": "2013-10-07t10:33:46.000 02:00",
        "type": "personbil",
        "use": "privat personk\u00f8rsel",
        "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": "gr\u00e5",
        "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
    }
]

Please also provide what "doesn't work"
If you need any help with formatting/how to ask/etc this is the guide.

To extract keys/values from this string you first need to parse it: you can use the JSON library for this.
You can then use inbuilt functions to take get the keys and items

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