Python工作表.write将2列串联为1列

2024-05-14 06:11:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个专栏:

  • 日期
  • 使用者

我试着在两个列中进行连接,最后只得到一个这样的列:

  • 日期/用户

但我不确定在使用Python工作表.write时如何做到这一点

这里有人有这门课的经验吗

当然,对于已经使用过此类的用户来说,这应该是一个简单的解决方案

import xlsxwriter

worksheet.write('A' + str(x), unicode(Date , 'utf-8'), headerBorderFormat)
worksheet.write('B' + str(x), unicode(Username, 'utf-8'), headerBorderFormat)
worksheet.write('C' + str(x), unicode(Date + ' / ' + Username, 'utf-8'), headerBorderFormat)

# get and display one row at a time.
for row in details:
    x += 1
    worksheet.write('A' + str(x), row[0], dateFormat)
    worksheet.write('B' + str(x), row[1], tableDataFormat)
    #here I have to concat row[0] + ' / ' + row[1]


workbook.close()

Tags: 用户importdateunicodeusername使用者经验解决方案
1条回答
网友
1楼 · 发布于 2024-05-14 06:11:09

如果row的第一个元素是datetime对象,第二个元素是字符串,那么在提供所需的日期格式(请参见https://strftime.org/)时,类似的内容可以工作:

# single column header:
col_name = unicode(Date , 'utf-8') + ' / ' + unicode(Username, 'utf-8')
worksheet.write('A' + str(x), col_name, headerBorderFormat)

# get and display one row at a time.
for row in details:
    x += 1
    format = '%Y%m%d'
    cell_content = row[0].strftime(format) + ' / ' + row[1]
    worksheet.write('A' + str(x), cell_content, tableDataFormat)

相关问题 更多 >