writing the output results on CSV file, python

I reached below list (data list in the end code) after calculation of the hash password from the csv file:

['danial', 5104, 'elham', 9770]

and i am going to write every name and password on csv file (update it) in below order:

danial,5104
elham,9770

I have used below code, but the output is false.

data=[]
for item in list3:
    #data=[]
    l3=[]
    l3.append(item)
    #print(l3)
    data.append(str(item))
print(data)

with open('code.csv', 'w', newline='') as output_file_name:
        writer = csv.writer(output_file_name)
        writer.writerows(data)
        output_file_name.close()

Each row needs to be a separate list. I.e. your data field currently looks like:

['danial', '5104', 'elham', '9770']

and to get it to output in your desired format, needs to look like:

[[ 'danial', '5104'], ['elham', '9770'] ]

(extra spaces for readability).

1 Like