Loop through a string-list and make key:pair in json format in Python


I have a csv file as:

devid,1, devType,"type-928"
devid,2, devType,"type-930" etc. 

Length of the lines might be different, depending on the number of key-pair values included. But must have ‘devid’ and ‘devType’ in them.

Want to pair them as key:value and format them in JSON.

Retrieved it from S3 as a ‘list’, which is comprised of strings separated by r.

Devices: ['devid,1,devType,"type-928"r', 'devid,2,devType,"type-930"']

for device in Devices:
    device_data = device.split(',')

Get device as ‘string’ separated by ‘,’ :


By split(‘,’)

device_data = device.split(',') 

device_data as:

   ['devid', '1', 'devType', '"oinet-928"r'] 
   ['devid', '2', 'devType', '"oinet-930"']


As ‘device_data’ is a list, I tried with ‘zip’.

for first, second in zip(device_data, device_data[1:]):
        print(first, ":" second)

But it is pairing as 1st and 2nd, 2nd and 3rd etc. How to get the 1st & 2nd, 3rd & 4th till the end.

devid : 1
1 : devType
devType : "oinet-928"
devid : 2
2 : devType
devType : "oinet-930"

Finally would like make a JSON formatted file, with Key:Value pairing two elements of the string. Such as:

          "devid": 1,
          "devType": "type-928"
          "devid": 2,
          "devType": "type-930"

There might be other values in the data, but there must be ‘devid’ and ‘devType’, else it will be ignored.

Another confusion is, how to put the commas for all the blocks after ‘}’ but except the last one.

Any helps would be very much appreciated.

Asked By: Fakir



import json
Devices = ['devid,1,devType,"type-928"r', 'devid,2,devType,"type-930"']
result = []
for device in Devices:
    device = device.strip()
    device_data = device.split(',')
    temp_dict = { device_data[0]: int(device_data[1]), device_data[2]: str(device_data[3][1:-1])}
result_dict = { "data": result }

print(json.dumps(result_dict, indent = 1))


 "data": [
   "devid": 1,
   "devType": "type-928"
   "devid": 2,
   "devType": "type-930"
Answered By: Uddhav Savani

This solves your question.

lines = [

data = []
for line in lines:
    entries = line.strip().split(',')
    keys = entries[0::2]
    values = entries[1::2]
    data.append({key: value for key, value in zip(keys, values)})


which yields

[{'devid': '1', 'devType': '"type-928"'},
 {'devid': '2', 'devType': '"type-930"'}]

It works by retrieving every second element of the list ( the ::2 is the key)

Answered By: Lukas Hestermeyer
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.