python-3.x - “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)
解决方案
HTML 文件的格式略有不同,我需要传递sort=False
给pd.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 版中的新内容。这解决了问题。
推荐阅读
- c++ - 意外调用了复制构造函数
- networking - 为什么 A-MSUD 机制在传输错误方面的鲁棒性不如 A-MPDU?
- angular - Angular 中的 JSPlumb 缩放/平移
- mysql - MySQL联合同一张表的两个查询
- reactjs - 使用 GatsbyImage 更改悬停图像的最佳方法
- pytorch - 在 pytorch 中使用 ImageFolder 和albumentations
- java - 如何解决 Spring security 上的不稳定行为让 userdetailsService 正确
- .net - ASPNET CORE Google Oauth 错误 400:Heroku 上的 redirect_uri_mismatch
- python - 如何在 pysimplegui 中更改背景颜色?
- python - Python - 如果 csv 已经存在,如何创建 csv