csvwriter, commas separators create undesirable columns between the actual values

25 Views Asked by At

I am trying to create a CSV file I can open with excel from an API data extraction (I don't know how to import it here) by using csvwriter, however for now commas separators are considered a value and added to columns between the actual values.

My code looks like this :

import csv
resources_list="resources_list.csv"

list_keys_res=[]
list_keys_res=list(res['items'][0].keys())

list_items_res=[]
list_items_res=list(res['items'])

resources = open(resources_list, 'w', newline ='')
with resources:
    # identifying header
    writer = csv.writer(resources, delimiter=';')
    header_res = writer.writerow([list_keys_res[1]]+[";"]+[list_keys_res[0]]+[";"]+[list_keys_res[2]]+[";"]+[list_keys_res[4]])
    resource=None
    
    # loop to write the data into each row of the csv file - a row describes 1 resource
    for i in list_items_res:
        resource=list_items_res.index(i)
        list_values_res=[]
        list_values_res=list(list_items_res[resource].values())

        writer.writerow([list_values_res[1]]+[";"]+[list_values_res[0]]+[';']+[list_values_res[2]]+[";"]+[list_values_res[4]])
        i=resource+1`

My goal is to have : name ; id ; userName ; email "Resource 1" ; 1 ; res1 ; [email protected] "Resource 2" ; 2 ; res2 ; [email protected] "Resource 3" ; 3 ; res3 ; [email protected] ...

And for now I have : name;";";id;";";userName;";";email Resource 1;";";1;";"; res1;";"; [email protected] Resource 2;";";2;";"; res2;";"; [email protected] Resource 3;";";3;";"; res3;";"; [email protected] ...

There is the look on Excel for now

The code works just fine I just can't seem to make the form right. Thanks in advance, I hope this will help others aswell!

0

There are 0 best solutions below