0

Hey I am using easygui and appending the user input to a excel (csv file). However the userinput will continuously append to the same line, and not the next line. Here is my code :

   #Adding a User

msg = 'Adding your information'
title = 'Uk Users'
box_names = ["Email" , "Password"]
box_values = (easygui.multpasswordbox(msg, title, box_names,))


while box_values[0] == '' or box_values[1] == '':
    msg = 'Try again'
    title = 'you missed a box, please try again!'
    box_names_1 = ["Email" , "Password"]
    box_values = str(easygui.multpasswordbox(msg, title, box_names_1))
    #How to make it repeat?
else:
    for i in range(len(box_values)):
       box_values = str(box_values)
    f = open('USERS.csv' , 'a') #'a' is for appending
    f.write(box_values) #How to add something to a new line?
  • You could use the `csv` library from python and use the `writerow` method there. – toti08 Nov 19 '18 at 14:42
  • Duplicate of [How do you append to a file?](https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file)? – bene Nov 19 '18 at 14:43

1 Answers1

0

You could use the csv library, defining a Writer object to write to your file. So you would need to replace the else statement with something like that:

else:
    with open('USERS.csv', 'a', newline = '') as csvFile:
        csvWriter = csv.writer(csvFile, delimiter = ',')
        csvWriter.writerow(box_values)

The writerow method will automatically put your new data into a new row. Also you don't need to explicitly convert your data to string.

toti08
  • 2,448
  • 5
  • 24
  • 36
  • Thanks, that does work although it does skip a line in the excel file, it is a lot better than what I had before. – Beni billhardt Nov 20 '18 at 02:16
  • @Benibillhardt right. Then in order to address that problem you should open the file specifying the parameter `newline=''`. I'll update my answer. – toti08 Nov 20 '18 at 07:19