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
3 个解决方案
#1
11
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:
如果你想找到一个spectific单元格,比如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]
.
然后用矩阵[5] [12]访问F13。
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.
P.S。:我没有测试过这个。如果“row”是一个列表,每个单元格都作为一个元素,则继续将所有行追加到矩阵中,因此第一个索引是行号,第二个索引是列号。
#2
2
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代码所在的目录,它的工作原理。
#3
2
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
您可以阅读以下内容以获得有关如何使用它的详细信息
https://automatetheboringstuff.com/chapter12/
https://automatetheboringstuff.com/chapter12/
#1
11
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:
如果你想找到一个spectific单元格,比如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]
.
然后用矩阵[5] [12]访问F13。
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.
P.S。:我没有测试过这个。如果“row”是一个列表,每个单元格都作为一个元素,则继续将所有行追加到矩阵中,因此第一个索引是行号,第二个索引是列号。
#2
2
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代码所在的目录,它的工作原理。
#3
2
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
您可以阅读以下内容以获得有关如何使用它的详细信息
https://automatetheboringstuff.com/chapter12/
https://automatetheboringstuff.com/chapter12/