Python - 写入Excel电子表格 [英] Python - Write to Excel Spreadsheet

查看:201
本文介绍了Python - 写入Excel电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手。我需要从我的程序写一些数据到电子表格。我在网上搜索,似乎有很多软件包(xlwt,XlsXcessive,openpyxl)。其他人建议写一个csv文件(从来没有使用csv&不真正明白它是什么)。



程序很简单。我有两个列表(float)和三个变量(字符串)。我不知道这两个列表的长度,它们可能不会是相同的长度。



我想要的布局如下图所示: / p>

>



粉红色列将具有第一个列表的值,绿色列将具有第二个列表的值。



最好的方法做到这一点?谢谢。



我正在运行Windows 7,但我不一定会在运行此程序的计算机上安装Office。



编辑
$ b

  import xlwt 

x = 1
y = 2
z = 3

list1 = [2.34,4.346,4.234]

book = xlwt.Workbook(encoding =utf-8)

sheet1 = book.add_sheet(Sheet 1)

sheet1.write(0,0,Display)
sheet1.write(1,0,Dominance)
sheet1.write(2,0,Test )

sheet1.write(0,1,x)
sheet1.write(1,1,y)
sheet1.write(2,1,z)

sheet1.write(4,0,Stimulus Time)
sheet1.write(4,1,Reaction Time)

i = 4

for list1中的n:
i = i + 1
sheet1.write(i,0,n)



book.save (trial.xls)

我使用所有建议写了这个。它得到完成的工作,但它可以稍微改进。如何将在for循环中创建的单元格(list1值)格式化为科学或数字?我不想截断值。

解决方案

  import xlwt 

def output(filename,sheet,list1,list2,x,y,z):
book = xlwt.Workbook()
sh = book.add_sheet )

variables = [x,y,z]
x_desc ='Display'
y_desc ='Dominance'
z_desc ='Test'
desc = [x_desc,y_desc,z_desc]

col1_name ='刺激时间'
col2_name ='反应时间'

#您可能需要将变量组合在一起
#for n,(v_desc,v)in enumerate(zip(desc,variables)):
for n,v_desc,v in enumerate(zip(desc,variables)):
sh。 write(n,0,v_desc)
sh.write(n,1,v)

n + = 1

sh.write )
sh.write(n,1,col2_name)

for m,e1 in enumerate(list1,n + 1):
sh.write(m,0,e1 )

for m,e2 in enumerate(list2,n + 1):
sh.write(m,1,e2)

book.save )

更多说明:
https://github.com/python-excel


I am new to Python. I need to write some data from my program to a spreadsheet. I've searched online and there seems to be many packages available (xlwt, XlsXcessive, openpyxl). Others suggest to write to a csv file (never used csv & don't really understand what it is).

The program is very simple. I have two lists (float) and three variables (strings). I don't know the lengths of the two lists and they probably won't be the same length.

I want the layout to be as in the picture below:

The pink column will have the values of the first list and the green column will have the values of the second list.

So what's the best way to do this? Thanks.

P.S. I am running Windows 7 but I won't necessarily have Office installed on the computers running this program.

EDIT

import xlwt

x=1
y=2
z=3

list1=[2.34,4.346,4.234]

book = xlwt.Workbook(encoding="utf-8")

sheet1 = book.add_sheet("Sheet 1")

sheet1.write(0, 0, "Display")
sheet1.write(1, 0, "Dominance")
sheet1.write(2, 0, "Test")

sheet1.write(0, 1, x)
sheet1.write(1, 1, y)
sheet1.write(2, 1, z)

sheet1.write(4, 0, "Stimulus Time")
sheet1.write(4, 1, "Reaction Time")

i=4

for n in list1:
    i = i+1
    sheet1.write(i, 0, n)



book.save("trial.xls")

I wrote this using all your suggestions. It gets the job done but it can be slightly improved. How do I format the cells created in the for loop (list1 values) as scientific or number? I do not want to truncate the values. The actual values used in the program would have around 10 digits after the decimal.

解决方案

import xlwt

def output(filename, sheet, list1, list2, x, y, z):
    book = xlwt.Workbook()
    sh = book.add_sheet(sheet)

    variables = [x, y, z]
    x_desc = 'Display'
    y_desc = 'Dominance'
    z_desc = 'Test'
    desc = [x_desc, y_desc, z_desc]

    col1_name = 'Stimulus Time'
    col2_name = 'Reaction Time'

    #You may need to group the variables together
    #for n, (v_desc, v) in enumerate(zip(desc, variables)):
    for n, v_desc, v in enumerate(zip(desc, variables)):
        sh.write(n, 0, v_desc)
        sh.write(n, 1, v)

    n+=1

    sh.write(n, 0, col1_name)
    sh.write(n, 1, col2_name)

    for m, e1 in enumerate(list1, n+1):
        sh.write(m, 0, e1)

    for m, e2 in enumerate(list2, n+1):
        sh.write(m, 1, e2)

    book.save(filename)

for more explanation: https://github.com/python-excel

这篇关于Python - 写入Excel电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆