如何将 Python 数组写入 Excel 电子表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31909722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write Python Array into Excel Spread sheet
提问by Felix
I am trying to write following array into Excel spreadsheet using Python:
我正在尝试使用 Python 将以下数组写入 Excel 电子表格:
array = [ [a1,a2,a3], [a4,a5,a6], [a7,a8,a9], [a10, a11, a12, a13, a14]]
At spreadsheet array should look:
在电子表格数组应该看起来:
a1 a4 a7 a10
a2 a5 a8 a11
a3 a6 a9 a12
a13
a14
Is anyone can show some Python code to do it? Thank you in advance,
有没有人可以展示一些 Python 代码来做到这一点?先感谢您,
Felix
菲利克斯
采纳答案by jmcnamara
Here is one way to do it using the XlsxWritermodule:
这是使用XlsxWriter模块执行此操作的一种方法:
import xlsxwriter
workbook = xlsxwriter.Workbook('arrays.xlsx')
worksheet = workbook.add_worksheet()
array = [['a1', 'a2', 'a3'],
['a4', 'a5', 'a6'],
['a7', 'a8', 'a9'],
['a10', 'a11', 'a12', 'a13', 'a14']]
row = 0
for col, data in enumerate(array):
worksheet.write_column(row, col, data)
workbook.close()
Output:
输出:
回答by bpgeck
The most common way that I've seen of writing to an excel spreadsheet with Python is by using OpenPyXL, a library non-native to python. Another that I've heard that is occasionally used is the XlsxWriter, again, though, it's non-native. Both sites have great documentation on how to best use the libraries but below is some simple code I wrote up to demonstrate OpenPyXL:
我见过的使用 Python 写入 excel 电子表格的最常见方法是使用OpenPyXL,这是一个非 Python 原生库。我听说偶尔使用的另一个是XlsxWriter,但同样,它不是本地的。这两个站点都有关于如何最好地使用这些库的很好的文档,但下面是我编写的一些简单代码来演示OpenPyXL:
from openpyxl import workbook
from openpyxl.cell import get_column_letter
workbook = Workbook() # the master workbook
output_file_name = "outfile.xlsx" # what "workbook" will be saved as
worksheet = workbook.active() # all workbooks have one worksheet already selected as the default
worksheet.title = "foo"
worksheet['A3'] = "=SUM(A1, A2)" # set up basic formula
wb.save(output_file_name)
EDIT:For example, your request could be written as such:
编辑:例如,您的请求可以这样写:
## imports and stuff ##
array = [ [a1,a2,a3], [a4,a5,a6], [a7,a8,a9], [a10, a11, a12, a13, a14]]
workbook = Workbook()
worksheet = workbook.active()
numrows = len(array)
letter = 'A'
for r in range(0, numrows):
if r == 0: letter = 'A'
if r == 1: letter = 'B'
if r == 2: letter = 'C'
...
numcols = len(array[r])
for c in range(0, numcols):
worksheet[letter.join(c)] = array[r][c]
Honestly this might not even work but I'm too tired to test. I think you get the idea though.
老实说,这甚至可能不起作用,但我太累了无法测试。我想你明白了。
回答by Sourya Dey
print array
This prints the array in the Python console with square brackets marking the beginning and end of rows. Select the whole of that and copy-paste to Excel. Click on the paste icon -> Text Import Wizard. That should bring up this.
这会在 Python 控制台中打印数组,并用方括号标记行的开头和结尾。选择整个并复制粘贴到 Excel。单击粘贴图标 -> 文本导入向导。那应该提出这个。
Choose Fixed Width and click Next to get this
选择固定宽度,然后单击下一步得到这个
Click Next and click Finish. That will do it. You'll still need to delete the ending brackets from some of the cells.
单击下一步,然后单击完成。这样就可以了。您仍然需要从某些单元格中删除结束括号。
回答by Jing Li
Use pandas data frame!
使用熊猫数据框!
import pandas as pd
array = [['a1', 'a2', 'a3'],
['a4', 'a5', 'a6'],
['a7', 'a8', 'a9'],
['a10', 'a11', 'a12', 'a13', 'a14']]
df = pd.DataFrame(array).T
df.to_excel(excel_writer = "C:/Users/Jing Li/Desktop/test.xlsx")
excel_writer is File path in str or existing ExcelWriter object.
excel_writer 是 str 或现有 ExcelWriter 对象中的文件路径。