首页 > 解决方案 > “ValueError:未找到表”:Python pd.read_html 未加载输入文件

问题描述

我正在尝试导入一系列 HTML 文件,其中包含我保存在工作目录中的新闻文章。我使用一个 HTML 文件开发了代码,并且运行良好。但是,我已经修改了代码以导入多个文件。

从下面的代码可以看出,我使用的是 pandas 和 pd.read_html()。它不再导入任何文件并给我错误代码“ValueError:未找到表”。

我尝试过使用不同类型的 HTML 文件,所以这似乎不是问题。我还更新了我正在使用的所有软件包。我在 Anaconda Navigator 中使用 OSX 和 Python 3.6 和 Pandas 0.20.3。

它工作正常,现在不行了。我究竟做错了什么?

任何提示或线索将不胜感激。

import pandas as pd
from os import listdir
from os.path import isfile, join, splitext
import os

mypath = 'path_to_my_wd'

raw_data = [f for f in listdir(mypath) if (isfile(join(mypath, f)) and splitext(f)[1]=='.html')]

news = pd.DataFrame()

for htmlfile in raw_data:
    articles = pd.read_html(join(mypath, htmlfile), index_col=0) #reads file as html
    data = pd.concat([art for art in articles if 'HD' in art.index.values], 
    axis=1).T.set_index('AN')
    data_export = pd.DataFrame(data, columns=['AN', 'BY', 'SN', 'LP', 'TD']) 
    #selects columns to export
    news = news.append(data_export)

标签: python-3.xpandashtml-table

解决方案


HTML 文件的格式略有不同,我需要传递sort=Falsepd.concat()data = pd.concat([art for art in articles if 'HD' in art.index.values], sort=False, axis=1).T.set_index('AN')这是 Pandas 0.23.0 版中的新内容。这解决了问题。


推荐阅读