I'm parsing a csv line by line. There are 3 columns in a row that need to be each split into lists and then combined into dictionaries in a list. The data inside each column is separated by two pipes: ||
incoming data:
buttons, clicked, id
choice 1 || choice 2 || choice 3, TRUE || FALSE || FALSE, choice 1 id|| choice 2 id || choice 3 id
('buttons','choice 1 || choice 2 || choice 3'),
('clicked', 'TRUE || FALSE || FALSE'),
('id','choice 1 id|| choice 2 id || choice 3 id'),
EDITED TO ADD:
input has a lot more columns that should not be included. only these 3 columns are included for this step.
i.e. sample output of a row that is being processed
print(data[0])
OrderedDict([
('pathId', 'test_id'),
('stepId', ''),
('nodeId', 'ROOT'),
('responseId', 'test_response'),
('responseUuid', ''),
('type', ''),
('language', 'en-US'),
('buttons','choice 1 || choice 2 || choice 3'),
('clicked', 'TRUE || FALSE || FALSE'),
('id','choice 1 id|| choice 2 id || choice 3 id'),
('state', 'resolved'),
('flags', 'accepted')])
the actual number of columns is dynamic and unpredictable. But in rows that contain values for buttons
, clicked
and id
will need this processing.
END EDIT
Expected output for this row needs to be:
buttonChoices =
[
{
"button": "choice 1",
"clicked": true,
"id": "choice 1 id"
},
{
"button": "choice 2",
"clicked": false,
"id": "choice 2 id"
},
{
"button": "choice 3",
"clicked": false,
"id": "choice 3 id"
}
]
For now I have:
for row in data:
buttonChoices = []
buttonText = row['button'].split('||')
buttonClicked = row['clicked'].split('||')
buttonId = row['id'].split('||')
but stuck for next step
You can use zip to combine the values 3 by 3 and to associate them to a key:
data = dict([ ('buttons','choice 1 || choice 2 || choice 3'),
('clicked', 'TRUE || FALSE || FALSE'),
('id','choice 1 id|| choice 2 id || choice 3 id')])
buttonChoices = [ dict(zip(data,map(str.strip,values)))
for values in zip(*(v.split("||") for v in data.values())) ]
print(buttonChoices)
[{'buttons': 'choice 1', 'clicked': 'TRUE', 'id': 'choice 1 id'},
{'buttons': 'choice 2', 'clicked': 'FALSE', 'id': 'choice 2 id'},
{'buttons': 'choice 3', 'clicked': 'FALSE', 'id': 'choice 3 id'}]
note: I added a map(str.strip,...) in there to clean up the messy string separations but you don't need it if your actual data is properly formatted
To generalize this a bit, you can define input keys and output keys to filter and rename as needed:
inKeys = ('buttons','clicked','id')
outKeys = ('button','clicked','id')
buttonChoices = [ dict(zip(outKeys,map(str.strip,values)))
for values in zip(*(data[k].split("||") for k in inKeys)) ]
print(buttonChoices)
[{'button': 'choice 1', 'clicked': 'TRUE', 'id': 'choice 1 id'},
{'button': 'choice 2', 'clicked': 'FALSE', 'id': 'choice 2 id'},
{'button': 'choice 3', 'clicked': 'FALSE', 'id': 'choice 3 id'}]
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다