如何将Python数组(data = [])写入Excel?
我正在编写一个 python 程序来处理 .hdf 文件,我想将此数据输出到 Excel 电子表格。我将数据放入一个数组中,如下所示:
代码:
data = []
for rec in hdfFile[:]:
data.append(rec)
从这里我创建了一个包含 9 列和 171 行的 2D 数组。
我正在寻找一种方法来迭代这个数组并将每个条目按顺序写入一张纸中。我想知道是否应该创建一个列表,或者如何使用 我创建的数组。
任何帮助将不胜感激。
I am writing a python program to process .hdf files, I would like to output this data to an excel spreadsheet. I put the data into an array as shown below:
Code:
data = []
for rec in hdfFile[:]:
data.append(rec)
from here I have created a 2D array with 9 columns and 171 rows.
I am looking for a way to iterate through this array and write each entry in order to a sheet. I am wondering if If I should create a list instead, or how to do this with
the array I have created.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就像@senderle所说,使用csv.writer
Just like @senderle said, use csv.writer
需要注意的一个重要文件类型是 CSV(即逗号分隔值文件)。它是一种非常简单的文本文件类型(通常已与 Excel 或其他电子表格应用程序关联),其中每个逗号分隔同一行上的多个单元格,文件中的每个新行代表新行上的数据。 IE:
上面的示例将导致第一行有 3 个单元格,每个单元格包含每个字母。新行指出 1、2 和 3 位于下一行,每个都在自己的单元格中。如果单元格中需要逗号,您可以将该单元格放在引号中。在我的例子中,“你好,世界!”将存在于第 3 行第 1 个单元格中。更正式的定义:http://www.csvreader.com/csv_format.php
A great file type to be aware of is a CSV, or Comma Separated Value file. It's a very simple text file type (normally already associated with Excel or other spreadsheet apps) where each comma separates multiple cells on the same row and each new line in the file represents data on a new row. I.E.:
The above example would result in the first row having 3 cells, each cell holding each letter. The new line states that 1, 2, and 3 are in the next row, each in their own cell. If a cell needs a comma in it, you can place that cell in quotes. In my example, "Hello, World!" would exist in the 3rd row, 1st cell. For a more formal definition: http://www.csvreader.com/csv_format.php
内置的解决方案是 python 的 csv 模块。您可以创建一个
csv.writer
并使用它将行追加到 .csv 文件中,该文件可以在 Excel 中打开。The built-in solution is python's
csv
module. You can create acsv.writer
and use that to append rows to a .csv file, which can be opened in excel.