skip to Main Content

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(',')
    print(device) 

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

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

By split(‘,’)

device_data = device.split(',') 

device_data as:

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

etc.

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:

{
   "data":[
        {
          "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.

2

Answers


  1. 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.append(temp_dict)
        
        
    result_dict = { "data": result }
    
    print(json.dumps(result_dict, indent = 1))
    
    
    

    Output

    {
     "data": [
      {
       "devid": 1,
       "devType": "type-928"
      },
      {
       "devid": 2,
       "devType": "type-930"
      }
     ]
    }
    
    Login or Signup to reply.
  2. This solves your question.

    lines = [
        'devid,1,devType,"type-928"', 
        'devid,2,devType,"type-930"'
    ]
    
    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)})
    
    data
    

    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)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search