I'm reading a sequence of files and writing them and their parameters in lists, then I want to create a csv which looks like this:
File1 | parameterA1 | parameterA2 | |
---|---|---|---|
File2 | parameterP1 | parameterP2 | parameterP3 |
What I get with my code:
file1 parameterA parameterA2 |
---|
file2 parameterP1 parameterP2 parameterP3 |
How can I get each parameter in its own cell? Any help appreciated. What's interesting, if I run the code with just 1 file - parameters put in separate cells.
import csv
files = ['f1', 'f2']
params_a = ['a1', 'a2']
params_p = ['p1', 'p2', 'p3']
with open ('csv.csv', 'a') as csvfile:
csvwriter = csv.writer(csvfile, delimiter = ',')
for file in files:
csvfile.write('\n%s\t' % (file) )
for parameter_a in params_a:
csvfile.write('%s\t' % (parameter_a))
for parameter_p in params_p:
csvfile.write('%s\t' % (parameter_p))
I tried playing with delemeter, other arguments and line endings but it got worse. Not sure what breaks the 'style' for multiple writing operations.
UPD: updated the table to match the code sample
The most straightforward answer based on the code you provided is this:
That outputs:
That params_a belongs to file "f1", and params_p to "f2", seems to be special knowledge you need to manually code.
If your data actually looked like this:
Then you can do something like this:
You can also dynmaically size a header, if you like:
and then insert it:
That outputs:
Your data could also look like this and something similar will work: