pythoncsvcsvwriter

How to write in separate cells with python csvwriter?


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


Solution

  • The most straightforward answer based on the code you provided is this:

    files = ["f1", "f2"]
    params_a = ["a1", "a2"]
    params_p = ["p1", "p2", "p3"]
    
    with open("output_manual.csv", "a", newline="") as csvfile:
        csvwriter = csv.writer(csvfile)
    
        row = [files[0]] + params_a
        csvwriter.writerow(row)
    
        row = [files[1]] + params_p
        csvwriter.writerow(row)
    

    That outputs:

    f1,a1,a2
    f2,p1,p2,p3
    

    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:

    files = [
        "f1",
        "f2",
    ]
    
    params = [
        ["a1", "a2"],
        ["p1", "p2", "p3"],
    ]
    

    Then you can do something like this:

    with open("output_mine.csv", "w", newline="") as f_out:
        writer = csv.writer(f_out)
        
        for i, file in enumerate(files):
            row = [file] + params[i]
            writer.writerow(row)
    

    You can also dynmaically size a header, if you like:

    max_param_len = 0
    for param in params:
        if len(param) > max_param_len:
            max_param_len = len(param)
    
    header = ["File"]
    for i in range(max_param_len):
        header += [f"Param_{i+1}"]
    

    and then insert it:

    ...
    writer = csv.writer(f_out)
    writer.writerow(header)
    ...
    

    That outputs:

    File,Param_1,Param_2,Param_3
    f1,a1,a2
    f2,p1,p2,p3
    

    Your data could also look like this and something similar will work:

    file_param_map = {
        "f1": ["a1", "a2"],
        "f2": ["p1", "p2", "p3"],
    }