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
Output
This solves your question.
which yields
It works by retrieving every second element of the list ( the
::2
is the key)