代码之家  ›  专栏  ›  技术社区  ›  Ashley Redman BSc

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

  •  9
  • Ashley Redman BSc  · 技术社区  · 10 年前

    我正在使用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,这很好,但在 工作表.write_string( 部分我需要知道如何指定如何将字符串写入一个新行,或者可能是一些条件格式,比如“如果该行已经包含文本,那么写入下一个自由行”等。

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

    谢谢:)

    2 回复  |  直到 10 年前
        1
  •  10
  •   Vaulstein MUGABA    7 年前

    更好的方法是使用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

        2
  •  3
  •   jmcnamara    10 年前

    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)
    

    请参见 Working with Cell Notation 部分。