首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取带有Pandas & xlrd返回错误的XLS文件;xlrd自己打开文件

读取带有Pandas & xlrd返回错误的XLS文件;xlrd自己打开文件
EN

Stack Overflow用户
提问于 2021-08-28 19:00:21
回答 1查看 6K关注 0票数 0

我正在用Python编写一些自动脚本来处理Excel文件,有些是XLS格式的。下面是我尝试使用Pandas的代码片段:

代码语言:javascript
复制
df = pd.read_excel(contents, engine='xlrd', skiprows=5, names=['some', 'column', 'headers'])

contents是从AWS S3桶中提取的文件内容。当这一行运行时,我得到了[ERROR] ValueError: File is not a recognized excel file

在排除此问题时,我尝试使用xlrd直接访问电子表格:

代码语言:javascript
复制
book = xlrd.open_workbook(file_contents=contents)
print("Number of worksheets is {}".format(book.nsheets))
print("Worksheet names: {}".format(book.sheet_names()))

这是没有错误的,因此xlrd似乎将它识别为Excel文件,只是当Pandas要求它这样做时就不会这样做了。

有人知道为什么Pandas不以xlrd作为引擎来读取文件吗?或者有人能帮我把xlrd的工作表转换成Pandas dataframe吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-28 22:05:59

或者有人能帮我把xlrd的工作表转换成Pandas吗?

pd.read_excel可以拿一本书..。

代码语言:javascript
复制
import xlrd

book = xlrd.open_workbook(filename='./file_check/file.xls')

df = pd.read_excel(book, skiprows=5)

print(df)

   some   column headers
0     1     some     foo
1     2  strings     bar
2     3     here     yes
3     4      too      no

如果您想检查/处理Excel文件类型,我将包括下面的代码。也许你可以根据你的需要来调整它。

代码循环遍历一个本地文件夹,并显示文件和扩展名,然后使用python-magic钻入其中。它也有一栏显示来自mimetypes的猜测,但这并不是很好。一定要放大帧的图像,并看到一些.xls并不是扩展程序说的那样。另外,.txt实际上是一个Excel文件。

代码语言:javascript
复制
import pandas as pd
import glob
import mimetypes
import os
# https://pypi.org/project/python-magic/
import magic

path = r'./file_check' # use your path
all_files = glob.glob(path + "/*.*")

data = []

for file in all_files:
    name, extension = os.path.splitext(file)
    data.append([file, extension, magic.from_file(file, mime=True), mimetypes.guess_type(file)[0]])

df = pd.DataFrame(data, columns=['Path', 'Extension', 'magic.from_file(file, mime=True)', 'mimetypes.guess_type'])

# del df['magic.from_file(file, mime=True)']

df

在那里,您可以根据文件的类型筛选文件:

代码语言:javascript
复制
xlsx_file_format = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

xls_file_format = 'application/vnd.ms-excel'

for file in all_files:
    if magic.from_file(file, mime=True) == xlsx_file_format:
        print('xlsx')
        #  DO SOMETHING SPECIAL WITH XLSX FILES
    elif magic.from_file(file, mime=True) == xls_file_format:
        print('xls')
        #  DO SOMETHING SPECIAL WITH XLS FILES
    else:
        continue

dfs = []

for file in all_files:
    if (magic.from_file(file, mime=True) == xlsx_file_format) or \
    (magic.from_file(file, mime=True) == xls_file_format):
        # who cares, it all works with this for the demo...
        df = pd.read_excel(file, skiprows=5, names=['some', 'column', 'headers'])
        dfs.append(df)
    
print('\nHow many frames did we get from seven files? ', len(dfs))

输出:

代码语言:javascript
复制
xlsx
xls
xls
xlsx

How many frames did we get from seven files?  4
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68967386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档