如何使用 xlwt 包将多个 python 列表写入一个 excel 文件?

How to write multiple python lists into an excel file using xlwt package?

我正在将制表符分隔的文本文件读取到 python 列表中,现在我想将列表项放入 excel 文件中。我的文本文件的快照如下

BGIOSGA014473   0.955233    0.271947    3.5125704641    1.81252716688   Upregulated
BGIOSGA000577   0.219720    0.118096    1.86052025471   0.895706096568  Upregulated
BGIOSGA019391   0.959914    0.037380    25.67988229 4.68256668442   Upregulated
EPlOING00000022268  0.514413    0.701827    0.732962681687  -0.448188348529 Downregulated
BGIOSGA024514   0.481147    0.058495    8.22543807163   3.04009251617   Upregulated

i wanted it to be in this excel format

我试过的是这个

import xlwt
style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on')
wb = xlwt.Workbook(encoding="utf-8")
ws = wb.add_sheet('test')

with open("out.txt") as outfile:
    for line in outfile:
        line=line.strip().split("\t")
        for i in range():
            for i,e in enumerate(line):
                ws.write(i,1,e)
wb.save('test.xls')

但是,我不明白。我想念如何更改行。谁能帮忙

我建议为此使用 pandas。代码就像

一样简单
import pandas as pd

使用 df = pd.DataFrame(dummy_for_data)

创建一个 pandas 数据框
df.to_excel(path)

针对您的问题:

  1. 您的代码不更新行索引
  2. ws.write()参数为(row, col, val)
  3. range()需要一个参数

这是一个工作示例。

import xlwt
style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on')
wb = xlwt.Workbook(encoding="utf-8")
ws = wb.add_sheet('test')

with open("data.txt") as outfile:
    row = 0
    for line in outfile:
        line = line.strip().split("\t")
        for col, val in enumerate(line):
            ws.write(row, col, val)
        row = row + 1

wb.save('data.xls')