Reputation: 109
I have a long string in this type of format [var1,var2,var3,...], [var1,var2,var3,...], ... (It's one giant string)
What would be the best way to turn this into a large CSV with each [var1,var2,var3,...] existing as a row in the CSV with each component var1, var2, var3, etc delimited from one another? I am using Python3 to solve this problem.
Upvotes: 1
Views: 149
Reputation: 41
As long as there's no malformed data you might get away with something like this:
>>>import numpy as np
>>>import ast
>>> s = '[1, 2, 3], [4,5,6], [7,8,9]' # Your string
>>> ll = np.vstack(ast.literal_eval(s)) # Converts to python
>>> ll
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Writing a list of lists to csv can be done pretty easily with the csv
package.
Upvotes: 1