Skip to content
Advertisement

Grouping data in a manner to export them as a CSV file

I have a messy data which seems like

JavaScript

I want to group them in a way that they would look like

JavaScript

I tried mystr.split() to end up in a list and then define the following function to group them in 3’s:

JavaScript

I was pretty sure that was going to work, however, I got the following output:

JavaScript

I don’t know why but I guess my code took every figure as an individual data rather than accepting them as pieces of a number. Now I cannot advance furthermore, any alternative solutions would be graciously appreciated.

Advertisement

Answer

try list comprehension method. Also never use list1 += list2. Insead use list.extend() if needed.

JavaScript

output

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement