Search code examples
pythoncsvrowcol

Writing 2 variables on the same row in a csv file


I am creating a registering system which (when registered), writes the username and password to a csv file. How do i get the two variables user_namev2 and user_passwordv2 to be written next to eachother on the csv file?

This is the code i am currently using

if re.match(user_password2v2, user_passwordv2):
    print("Passwords do match")
    with open ('Accounts.csv', 'a') as Account_file:
        writer = csv.writer(Account_file)
        writer.writerow([user_namev2])
        writer.writerow([user_passwordv2])
        Bottom()

This code writes the username and password below eachother, when I would like it to be next to eachother on the same row, just different columns.

I have also experimented with adding it all to an array and writing it the array to the csv file, however this caused troubles when it came to reading the csv file (posted a question about this. See link below)

    if re.match(user_password2v2, user_passwordv2):
        print("Passwords do match")
        user_info = []
        user_info.append(user_namev2)
        user_info.append(user_passwordv2)
        with open ('Accounts.csv', 'a') as Account_file:
            writer = csv.writer(Account_file)
            writer.writerow([user_info])

Searching for only the first value in an array in a csv file


Solution

  • I believe that

    writer.writerow([user_namev2, user_passwordv2])

    should do the trick. (https://realpython.com/python-csv/)