Python Xlsx Writer – 将string写入新行

我正在使用xlsxwriter工具从用户input中将一些数据写入到.xlsx。 (我对编程一般都很陌生,所以如果我的代码很糟糕的话,我很抱歉,如果你提出/纠正了我所犯的任何错误,我将不胜感激!

以下是当前的代码:

不过,我将这些数据写入一个新行而不是取代之前的一些问题,所以.xlsx最终成为用户数据的集合。

import getpass import os import xlsxwriter print('Hello, please answer the follow questions to complete your registration.') userfirstname = input('Please enter your FIRST name.\n') print('Thank you, %s' % userfirstname, ) userlastname = input('Please enter your LAST name.\n') print('Thank you %s' % userfirstname, '%s' % userlastname) userage = input('Please enter your AGE.\n') print('Thank you %s' % userfirstname, '%s' % userlastname) username = input('Please enter your desired USER NAME.\n') print('Thank you %s' % userfirstname, '%s' % userlastname, 'Your chosen username is %s' % username) #Within this section, a database scan must take place to check current used usernames and text match userpwd = getpass.getpass('Please enter your desired PASSWORD.\n') print('Thank you %s' % userfirstname, '%s' % userlastname) #within this section, a application password authentication must take place to fit password criteria usermailid = input('Please enter the email address you wish to register with.\n') print('Your chosen registration email address is: %s' % usermailid) #Within this section, a database scan must take place to check current registered mail addresses and text match #User Database .xlsx workbook =xlsxwriter.Workbook('UserDatabase.xlsx') worksheet = workbook.add_worksheet() worksheet.set_column('A:A',20) worksheet.set_column('B:B',20) worksheet.set_column('C:C',20) worksheet.set_column('D:D',20) worksheet.set_column('E:E',20) worksheet.set_column('F:F',20) worksheet.write('A1', 'First Name') worksheet.write('B1', 'Last Name') worksheet.write('C1', 'Age') worksheet.write('D1', 'User Name') worksheet.write('E1', 'Password') worksheet.write('F1', 'Email ID') worksheet.write_string( , userfirstname) worksheet.write_string('', userlastname) worksheet.write_string('', userage) worksheet.write_string('', username) worksheet.write_string('', userpwd) worksheet.write_string('', usermailid) workbook.close() 

所以从这里,一个可爱的.xlsx是用列标题创build的,用户数据对第二行很好,但是在worksheet.write_string中(我需要知道如何指定如何将string写入新行或者可能是一些条件格式说'如果行已经包含文本,然后写入下一个空闲行'等

如果你知道一个更好的工具,而不是xlsxwriter,请随时让我知道!

谢谢 :)

write_方法中使用(row,col)符号是可能的,而且通常更有用。

像这样的东西:

 row = 0 col = 0 worksheet.write(row, col, 'First Name') worksheet.write(row, col + 1, 'Last Name') worksheet.write(row, col + 2, 'Age') worksheet.write(row, col + 3, 'User Name') worksheet.write(row, col + 4, 'Password') worksheet.write(row, col + 5, 'Email ID') row += 1 worksheet.write_string(row, col, userfirstname) worksheet.write_string(row, col + 1, userlastname) worksheet.write_string(row, col + 2, userage) worksheet.write_string(row, col + 3, username) worksheet.write_string(row, col + 4, userpwd) worksheet.write_string(row, col + 5, usermailid) 

请参阅文档中的使用单元格表示法部分。

更好的方法是使用write_row。

你的代码将被缩减为:

row = 0 col = 0 rowHeaders = ['First Name','Last Name','Age','User Name','Password','Email ID'] rowValues = [userfirstname,userlastname,userage,username,userpwd,usermailid ] worksheet.write_row(row, col, tuple(rowHeaders)) row += 1 worksheet.write_row(row, col, tuple(rowValues))

检查链接以获取更多文档: write_row