繁体   English   中英

Python:不支持的格式,或损坏的文件

[英]Python: Unsupported format, or corrupt file

I am trying to make a python program that downloads and XLS file from a website, in this case website is: https://www.blackrock.com/uk/individual/products/291392/ , and loads it as a dataframe in pandas ,具有正确的数据结构。

问题是当我尝试通过 pandas 加载它时,它给了我一个错误:XLRDError:不支持的格式,或损坏的文件:预期的 BOF 记录; 找到 b'\xef\xbb\xbf\xef\xbb\xbf<?'

我不太确定是什么导致了这个错误,但可能是文件中的某些东西。 我可以在 Excel 中打开文件,即使我收到文件和文件扩展名不匹配的警告,并且文件可能很危险等。如果我单击“是”以打开它,它会打开并正确显示数据. 如果我使用 Excel 将文件另存为.xlsx,我可以在 pandas 中打开它,但我宁愿使用不需要手动打开 Excel 并保存文件的解决方案。

我尝试将文件扩展名重命名为 xlsx,但这不起作用,因为它不允许我打开具有该扩展名的文件。 我尝试了许多不同的扩展,但没有一个会咬人——不幸的是。

我很茫然。

我希望,你能帮忙。

编辑:我使用的代码是:

download_path = 'https://www.blackrock.com/uk/individual/products/291392/fund/1527484370694.ajax?fileType=xls&fileName=iShares-MSCI-World-SRI-UCITS-ETF-USD-Dist_fund&dataType=fund'

testing = pd.read_excel(download_path, engine='xlrd', sheet_name = 'Holdings', skiprows = 3)

实际问题是文件格式是SpreadSheetML,它仅在2003 年至2006 年之间短暂使用过。它已被XLSX 格式取代。 因为,它已经存在了很短的时间,而且不久前,大多数包不支持加载/保存操作。 有关格式的更多信息,请参见: https://docs.microsoft.com/en-us/previous-versions/office/developer/office-xp/aa140066(v=office.10)?redirectedfrom=MSDN

由于这个原因,Pandas 或任何其他 XML 解析器(例如 Etree)将无法正确加载。 常规的 MS Office 软件仍然可以正确加载它。 据我所知,您可以使用aspose-cells package: https://products.aspose.com/cells/python-java/处理 SpreadSheetML 文件

对于您的情况:

# Import packages

import jpype
import asposecells
jpype.startJVM()
from asposecells.api import Workbook, FileFormatType
from asposecells.api import HtmlSaveOptions

# Read Workbook

workbook = Workbook('iShares-MSCI-World-SRI-UCITS-ETF-USD-Dist_fund.xls')
worksheet = workbook.getWorksheets().get(0)

# Accessing a cell using its name

cells = worksheet.getCells()
cell = cells.get("A1")

# Print Message

print("Cell Value: " + str(cell.getValue())) # Prints Cell Value: 17-Nov-2021

# To save SpreadSheetML in different format (HTML)

saveOptions = HtmlSaveOptions()
saveOptions.setDisableDownlevelRevealedComments(True)
workbook.save("iShares-MSCI-World-SRI-UCITS-ETF-USD-Dist_fund.html", saveOptions)

正如 Slybot 所提到的,这不是一个真正的 xls 文件。

如果您在纯文本编辑器或十六进制编辑器中检查内容,header 将启动:

<?xml version="1.0"?>
<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">

确认这是 xml 文档,而不是 Office 2007 压缩 xlsx 办公文档。

您的下一步取决于您是否在将运行此代码的机器上安装了 Excel,如果没有,您有权访问并愿意支付的其他库 - 例如,Slybot 提到aspose

最简单的解决方案 - Excel

如果您在安装了 Excel 的 Windows 机器上运行此程序,则您可以免费且有能力选择自动打开 Excel 并保存为 xlsx 的操作。 这是通过使用 Win32com 模块,在这个答案中描述:

尝试使用 Python 解析 XLS (XML) 文件

Alternatively, save your Excel styled XML as xlsx with Workbook.SaveAs method using win32com (only for Windows users) and read in with pandas.read_excel skipping appropriate rows.

XML 解决方案

您可以阅读原始 XML 并消化它。 相关节点有:

<ss:Workbook>  
<ss:Worksheet ss:Name="Holdings">
<ss:Table>
<ss:Row>
<ss:Cell ss:StyleID="Left">
<ss:Data ss:Type="String">iShares MSCI World SRI UCITS ETF</ss:Data>

第三方库解决方案

我不熟悉提供此功能的任何库,因此无法就此选项提供建议。

读取 pandas / python 中的 xls 文件:不支持的格式,或损坏的文件:预期的 BOF 记录; 找到 b'\xef\xbb\xbf <!--?xml'</div--><div id="text_translate"><p> 我正在尝试将xls文件(只有一个选项卡)打开到 pandas dataframe 中。</p><p> It is a file that i can normally read in excel or excel for the web, in fact here is the raw file itself: <a href="https://www.dropbox.com/scl/fi/zbxg8ymjp8zxo6k4an4dj/product-screener.xls?dl=0&amp;rlkey=3aw7whab78jeexbdkthkjzkmu" rel="nofollow noreferrer">https://www.dropbox.com/scl/fi/zbxg8ymjp8zxo6k4an4dj/product-screener.xls?dl= 0&amp;rlkey=3aw7whab78jeexbdkthkjzkmu</a> 。</p><p> 我注意到前两行合并了单元格,一些列也是如此。</p><p> 我尝试了几种方法(来自堆栈),但都失败了。</p><pre> # method 1 - read excel file = "C:\\Users\\admin\\Downloads\\product-screener.xls" df = pd.read_excel(file) print(df)</pre><p> 错误: Excel file format cannot be determined, you must specify an engine manually.</p><pre> # method 2 - pip install xlrd and use engine file = "C:\\Users\\admin\\Downloads\\product-screener.xls" df = pd.read_excel(file, engine='xlrd') print(df)</pre><p> 错误: Unsupported format, or corrupt file: Expected BOF record; found b'\xef\xbb\xbf&lt;?xml' Unsupported format, or corrupt file: Expected BOF record; found b'\xef\xbb\xbf&lt;?xml'</p><pre> # method 3 - rename to xlsx and open with openpyxl file = "C:\\Users\\admin\\Downloads\\product-screener.xlsx" df = pd.read_excel(file, engine='openpyxl') print(df)</pre><p> 错误: File is not a zip file (可以选择转换,而不是重命名)。</p><pre> # method 4 - use read_xml file = "C:\\Users\\admin\\Downloads\\product-screener.xls" df = pd.read_xml(file) print(df)</pre><p> 此方法实际上会产生结果,但会产生与工作表没有任何意义的 DataFrame。 大概需要解释 xml (似乎很复杂)?</p><pre> Style Name Table 0 NaN None NaN 1 NaN All funds NaN # method 5 - use read_table file = "C:\\Users\\admin\\Downloads\\product-screener.xls" df = pd.read_table(file) print(df)</pre><p> 此方法将文件读入一列(系列)DataFrame。 那么如何使用这些信息来创建与 xls 文件形状相同的标准 2d DataFrame 呢?</p><pre> 0 &lt;Workbook xmlns="urn:schemas-microsoft-com:off... 1 &lt;Styles&gt; 2 &lt;Style ss:ID="Default"&gt; 3 &lt;Alignment Horizontal="Left"/&gt; 4 &lt;/Style&gt;... ... 226532 &lt;/Cell&gt; 226533 &lt;/Row&gt; 226534 &lt;/Table&gt; 226535 &lt;/Worksheet&gt; 226536 &lt;/Workbook&gt; # method 5 - use read_html file = "C:\\Users\\admin\\Downloads\\product-screener.xls" df = pd.read_html(file) print(df)</pre><p> 这将返回一个空白列表[] ,而人们可能期望至少有一个 DataFrame 列表。</p><p> 所以问题是将这个文件读入 dataframe (或类似的可用格式)的最简单方法是什么?</p></div>

[英]read xls file in pandas / python: Unsupported format, or corrupt file: Expected BOF record; found b'\xef\xbb\xbf<?xml'

如何解决错误:不支持的格式,或损坏的文件:预期的 BOF 记录; 找到 b'<div id="text_translate"><p> 当我运行此代码时显示此错误不支持的格式或损坏的文件:预期的 BOF 记录; 找到 b'</p><p> 数据: <a href="https://github.com/DevangBaroliya/DataSet/blob/master/DistrictWiseReport20200607.xlsx" rel="nofollow noreferrer">https://github.com/DevangBaroliya/DataSet/blob/master/DistrictWiseReport20200607.xlsx</a></p><pre> import pandas as pd data = pd.read_excel('DistrictWiseReport.xlsx') data</pre> </div><table c'< div></table>

[英]How Can I Solve Error : Unsupported format, or corrupt file: Expected BOF record; found b'<table c'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python xlrd 格式不受支持或文件损坏。 Python - XLRDError:不支持的格式,或损坏的文件:预期的 BOF 记录 Python pd.read_excel 不支持的格式,或损坏的文件 XLRDError:不支持的格式或损坏的文件:预期的BOF记录; 错误:不支持的格式,或损坏的文件:预期的 BOF 记录 无法在Python,xlrd.biffh.XLRD中打开.xls文件。错误:格式不受支持,或者文件损坏:预期的BOF记录; 找到了 读取 pandas / python 中的 xls 文件:不支持的格式,或损坏的文件:预期的 BOF 记录; 找到 b'\xef\xbb\xbf <!--?xml'</div--><div id="text_translate"><p> 我正在尝试将xls文件(只有一个选项卡)打开到 pandas dataframe 中。</p><p> It is a file that i can normally read in excel or excel for the web, in fact here is the raw file itself: <a href="https://www.dropbox.com/scl/fi/zbxg8ymjp8zxo6k4an4dj/product-screener.xls?dl=0&amp;rlkey=3aw7whab78jeexbdkthkjzkmu" rel="nofollow noreferrer">https://www.dropbox.com/scl/fi/zbxg8ymjp8zxo6k4an4dj/product-screener.xls?dl= 0&amp;rlkey=3aw7whab78jeexbdkthkjzkmu</a> 。</p><p> 我注意到前两行合并了单元格,一些列也是如此。</p><p> 我尝试了几种方法(来自堆栈),但都失败了。</p><pre> # method 1 - read excel file = "C:\\Users\\admin\\Downloads\\product-screener.xls" df = pd.read_excel(file) print(df)</pre><p> 错误: Excel file format cannot be determined, you must specify an engine manually.</p><pre> # method 2 - pip install xlrd and use engine file = "C:\\Users\\admin\\Downloads\\product-screener.xls" df = pd.read_excel(file, engine='xlrd') print(df)</pre><p> 错误: Unsupported format, or corrupt file: Expected BOF record; found b'\xef\xbb\xbf&lt;?xml' Unsupported format, or corrupt file: Expected BOF record; found b'\xef\xbb\xbf&lt;?xml'</p><pre> # method 3 - rename to xlsx and open with openpyxl file = "C:\\Users\\admin\\Downloads\\product-screener.xlsx" df = pd.read_excel(file, engine='openpyxl') print(df)</pre><p> 错误: File is not a zip file (可以选择转换,而不是重命名)。</p><pre> # method 4 - use read_xml file = "C:\\Users\\admin\\Downloads\\product-screener.xls" df = pd.read_xml(file) print(df)</pre><p> 此方法实际上会产生结果,但会产生与工作表没有任何意义的 DataFrame。 大概需要解释 xml (似乎很复杂)?</p><pre> Style Name Table 0 NaN None NaN 1 NaN All funds NaN # method 5 - use read_table file = "C:\\Users\\admin\\Downloads\\product-screener.xls" df = pd.read_table(file) print(df)</pre><p> 此方法将文件读入一列(系列)DataFrame。 那么如何使用这些信息来创建与 xls 文件形状相同的标准 2d DataFrame 呢?</p><pre> 0 &lt;Workbook xmlns="urn:schemas-microsoft-com:off... 1 &lt;Styles&gt; 2 &lt;Style ss:ID="Default"&gt; 3 &lt;Alignment Horizontal="Left"/&gt; 4 &lt;/Style&gt;... ... 226532 &lt;/Cell&gt; 226533 &lt;/Row&gt; 226534 &lt;/Table&gt; 226535 &lt;/Worksheet&gt; 226536 &lt;/Workbook&gt; # method 5 - use read_html file = "C:\\Users\\admin\\Downloads\\product-screener.xls" df = pd.read_html(file) print(df)</pre><p> 这将返回一个空白列表[] ,而人们可能期望至少有一个 DataFrame 列表。</p><p> 所以问题是将这个文件读入 dataframe (或类似的可用格式)的最简单方法是什么?</p></div> 如何解决错误:不支持的格式,或损坏的文件:预期的 BOF 记录; 找到 b'<div id="text_translate"><p> 当我运行此代码时显示此错误不支持的格式或损坏的文件:预期的 BOF 记录; 找到 b'</p><p> 数据: <a href="https://github.com/DevangBaroliya/DataSet/blob/master/DistrictWiseReport20200607.xlsx" rel="nofollow noreferrer">https://github.com/DevangBaroliya/DataSet/blob/master/DistrictWiseReport20200607.xlsx</a></p><pre> import pandas as pd data = pd.read_excel('DistrictWiseReport.xlsx') data</pre> </div><table c'< div></table> Pandas.read_excel:不支持的格式,或损坏的文件:预期的 BOF 记录 xlrd 读取 xls XLRDError:不支持的格式,或损坏的文件:预期的 BOF 记录; 找到 &#39;\r\n<html> &#39;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM