python - 使用列号中的条件使用 python 和 xlwt 导出到 Excel

标签 python export-to-excel xlwt

我使用 Python (xlwt) 将列表导出为 excel:

response = HttpResponse(mimetype="application/ms-excel")
response['Content-Disposition'] = 'attachment; filename=MyList.xls'

wb = xlwt.Workbook()
ws = wb.add_sheet('my_list')

ws.write(0, 0, 'Country ID')
if var =='with_flag':
    ws.write(0, X, 'Country Flag')
ws.write(0, 1, 'Country Name')

如果我在没有标志的情况下导出,则导出通过了镍;),但问题是当我选择使用标志导出时。我应该尊重列的顺序(1- country_name、2- country_flag、3-country_id)。我知道Python不支持++。我的代码中的 X 是否需要设置任何条件才能按特定顺序导出?

最佳答案

如果我理解你的问题,你想要这样的东西:

X = 1
if var =='with_flag':
    ws.write(0, X, 'Country Flag')
    X += 1
ws.write(0, X, 'Country Name')

关于python - 使用列号中的条件使用 python 和 xlwt 导出到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16772066/

相关文章:

Python - 如何使用 DateTime 重命名文本文件

python - 在条形图上添加条形高度值和缺失值

python - 如何计算numpy数组的两行之间的欧氏距离

asp.net-mvc - 无法导出 Kendo Grid 中的隐藏列

python - 使用 xlwt 将数组从特定列开始写入新工作簿

python - 将列转置为行,将前列的 value_counts 显示为 Pandas 中的列值

javascript - Angularjs 导出到 Excel 在 IE 中不起作用

c++ - 代码覆盖率结果 : Export to Excel

python - 从 Python 轻松编写格式化的 Excel : Start with Excel formatted, 在 Python 中使用它,并从 Python 重新生成 Excel

excel - Pandas to_excel( ) 输出 float 不正确