Python Xlsx Writer - 将字符串写入新行

9
我正在使用xlsxwriter工具根据用户输入将一些数据写入.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文件被创建了,并包含列标题和用户数据放在第二行,这很棒,但在工作表的 worksheet.write_string( 部分中,我需要知道如何指定如何将字符串写入新行,或者使用条件格式来表示“如果该行已包含文本,则写入到下一个可用行”等。

如果您知道比xlsxwriter更好的工具,请随时告诉我!

谢谢:)


相关:https://dev59.com/c2Ij5IYBdhLWcg3wOCyS#20508045 - jonrsharpe
谢谢,但我看到了这个,我认为这不是他们想要的情况,因为他们希望他们的代码读取先前创建的 .xlsx 文件,而这是在创建文件时指定的内容。 - Ashley Redman BSc
但是重点是XLSXWriter是只写的,所以您无法读取现有文件或(据我所知)从单元格读取以确定它是否为空。 - jonrsharpe
啊,我明白了,所以只有在指定实际单元格ID时,xlsxwriter才能将数据写入文件?我想,使用这个工具可能有更好的方法。 - Ashley Redman BSc
可能,该答案链接到他们网站上建议其他工具的页面。 - jonrsharpe
好的,谢谢您的回复,我会查看该页面上的链接,并在几分钟内关闭此问题。 - Ashley Redman BSc
2个回答

11

用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

3

write_方法中使用(行,列)表示法通常更加实用。

可以像这样:

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)

请参阅文档中的细胞符号使用部分。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接