在 python 中打开和读取 excel .xlsx 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18532893/
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
Opening and reading an excel .xlsx file in python
提问by Ryflex
I'm trying to open an excel .xlsx file with python but am unable to find a way to do it, I've tried using pandas but it's wanting to use a library called NumPy I've tried to install numpy but it still can't find numpy.
我正在尝试使用 python 打开一个 excel .xlsx 文件,但无法找到方法,我尝试使用 Pandas,但它想使用名为 NumPy 的库我尝试安装 numpy 但它仍然可以找不到麻木。
I've also tried using the xlrd library but I get the following traceback:
我也试过使用 xlrd 库,但我得到以下回溯:
Traceback (most recent call last):
File "C:\test.py", line 3, in <module>
book = open_workbook('test.xlsx')
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 370, in open_workbook
biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 1323, in getbof
raise XLRDError('Expected BOF record; found 0x%04x' % opcode)
XLRDError: Expected BOF record; found 0x4b50
Which I assume is because XLRD can't read .xlsx files?
我认为这是因为 XLRD 无法读取 .xlsx 文件?
Anyone got any ideas?
有人有任何想法吗?
EDIT:
编辑:
import csv
with open('test.csv', 'rb') as csvfile:
data = csv.reader(csvfile, delimiter=',')
for row in data:
print "------------------"
print row
print "------------------"
for cell in row:
print cell
采纳答案by Thales MG
Maybe you could export your .xlsx to a .csv file?
也许您可以将 .xlsx 导出为 .csv 文件?
Then you could try:
那你可以试试:
import csv
with open('file.csv','rb') as file:
contents = csv.reader(file)
[x for x in contents]
This may be useful: http://docs.python.org/2/library/csv.html#csv.reader
这可能很有用:http: //docs.python.org/2/library/csv.html#csv.reader
Hope that helps!
希望有帮助!
EDIT:
编辑:
If you want to locate a spectific cell, such as F13, you could make a nested list like a matrix and them refer to each element:
如果你想定位一个特定的单元格,比如 F13,你可以创建一个像矩阵一样的嵌套列表,它们引用每个元素:
import csv
with open('file.csv','rb') as file:
contents = csv.reader(file)
matrix = list()
for row in contents:
matrix.append(row)
And then access F13 with matrix[5][12]
.
然后使用matrix[5][12]
.
P.S.: I did not test this. If "row" is a list with each cell as an element, you keep appending all lines to the matrix, so the first index is row number and the second is the column number.
PS:我没有测试这个。如果“row”是一个以每个单元格为元素的列表,您将继续将所有行附加到矩阵中,因此第一个索引是行号,第二个是列号。
回答by toufikovich
it seems that you are on a Linux Distro. I had the same problem too and this does not happen with "xlwt" library but only with "xlrd". what I did is not the right way to solve this problem but it makes things work for the time being to hopefully have an answer to that question soon ;I have installed "xlrd" on Windows and took the folder and pasted it on Linux in the directory where my python code is and it worked.
看来您使用的是 Linux 发行版。我也有同样的问题,这不会发生在“xlwt”库中,而只会发生在“xlrd”上。我所做的不是解决这个问题的正确方法,但它使事情暂时起作用,希望很快能得到这个问题的答案;我已经在 Windows 上安装了“xlrd”,并把这个文件夹粘贴到 Linux 上我的 python 代码所在的目录,它可以工作。
回答by Elisma
Since I know other people will also be reading this -
因为我知道其他人也会阅读这个 -
You can install the following module (it's not there automatically) https://pypi.python.org/pypi/openpyxl
您可以安装以下模块(它不会自动安装) https://pypi.python.org/pypi/openpyxl
You can read the following to get a nice breakdown on how to use it
您可以阅读以下内容以了解如何使用它