如何在Excel文件中将内容附加到换行符?

时间:2018-11-19 14:35:59

标签: python file append

嘿,我正在使用easygui并将用户输入附加到excel(csv文件)中。但是,用户输入将连续追加到同一行,而不是下一行。 这是我的代码:

   #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?

1 个答案:

答案 0 :(得分:0)

您可以使用csv库,定义一个Writer对象以写入文件。因此,您需要使用以下内容替换else语句:

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

writerow方法将自动将新数据放入新行。另外,您无需将数据显式转换为字符串。