在python中打开并阅读excel .xlsx文件 [英] Opening and reading an excel .xlsx file in python

查看:1486
本文介绍了在python中打开并阅读excel .xlsx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用python打开一个excel .xlsx文件,但无法找到一种方法,我尝试使用大熊猫,但是想要使用一个名为NumPy的库我试图安装numpy,但是它仍然找不到numpy。



我也尝试使用xlrd库,但我得到以下追溯:

 追溯(最近的最后一次呼叫):
文件C:\test.py,第3行在< module>
book = open_workbook('test.xlsx')
文件C:\Python27\lib\site-packages\xlrd\__init __。py,第370行,在open_workbook
biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
文件C:\Python27\lib\site-packages\xlrd\__init __。py,第1323行,getbof
raise XLRDError ('预期BOF记录;找到0x%04x'%操作码)
XLRDError:预期的BOF记录;发现0x4b50

我认为是因为XLRD无法读取.xlsx文件?



任何人都有任何想法?



编辑:

 code $ c 
code code code $ $ $ $ $ $ $ $ $ $ $ $ $ $

$对于数据行:
print------------------
打印行
打印-------- ----------
行中的单元格:
打印单元


解决方案

也许你可以将.xlsx导出到.csv文件?



然后你可以尝试: p>

  import csv 
with open('file.csv','rb')as file:
contents = csv.reader(file)
[x for x in contents]

这可能是有用的:
http://docs.python.org/2/库/ csv.html#csv.re ader



希望有帮助!



编辑:



如果你想要找到一个像F13这样的细胞,你可以像矩阵一样做一个嵌套的列表,它们是指每个元素:

  import csv 
with open('file.csv','rb')as file:
contents = csv.reader(file)
matrix = list ()
内容中的行:
matrix.append(row)

然后使用 matrix [5] [12] 访问F13。



P.S .:我没有测试这个。如果row是每个单元格作为元素的列表,则将所有行继续附加到矩阵,因此第一个索引是行号,第二个是列号。


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.

I've also tried using the xlrd library but I get the following traceback:

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?

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

解决方案

Maybe you could export your .xlsx to a .csv file?

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

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:

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].

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.

这篇关于在python中打开并阅读excel .xlsx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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