Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将每个包含表的html文件列表解析为excel工作表_Python_Html_Pandas_Dataframe_Append - Fatal编程技术网

Python 将每个包含表的html文件列表解析为excel工作表

Python 将每个包含表的html文件列表解析为excel工作表,python,html,pandas,dataframe,append,Python,Html,Pandas,Dataframe,Append,我试图将html文件列表(每个文件都包含一个表)解析为excel工作表。 我成功地解析了两个html文件,并附加了它们的结果数据帧,如下所示: import pandas as pd #Read the first html and adjust header columns df = pd.read_html(r'C:\Users\A.html')[0].transpose() new_header = df.iloc[0] df = df[1:] df.columns = new_he

我试图将html文件列表(每个文件都包含一个表)解析为excel工作表。 我成功地解析了两个html文件,并附加了它们的结果数据帧,如下所示:

import pandas as pd
#Read the first html and adjust header columns

df = pd.read_html(r'C:\Users\A.html')[0].transpose()
new_header = df.iloc[0] 
df = df[1:] 
df.columns = new_header 

#Read the second html and adjust header columns

df1 = pd.read_html(r'C:\Users\B.html')[0].transpose()
new_header = df1.iloc[0] 
df1 = df1[1:] 
df1.columns = new_header 

df.append(df1) 
df1.to_excel(r'C:\Users\My Workbook.xlsx', sheet_name='Sheet 1', index = False)
但是,当我按如下方式添加循环时,追加不起作用:

import os
path = r'C:\Users'

# create a main dataframe object to append the other dataframes to 

df = pd.read_html(r'C:\Users\A.html')[0].transpose()
new_header = df.iloc[0] 
df = df[1:] 
df.columns = new_header 

#loop over the html files in the user path

with os.scandir(path) as it:
    for entry in it:
        if entry.name.endswith(".html") and entry.is_file():

            df_temp = pd.read_html(entry.path)[0].transpose()
            new_header = df_temp.iloc[0] 
            df_temp = df_temp[1:] 
            df_temp.columns = new_header 

            df.append(df_temp)

非常感谢您的帮助。

您的问题可能是
df.append(df_temp)
函数没有将数据附加到原始数据帧中。您需要将它再次保存到它自己的变量中,如:
df=df.append(df\u temp)


所以我有点惊讶你说你的第一个例子是有效的?请注意,您正在做一些非常类似的事情,但除此之外,您只使用
df1.to_excel()

将第二个数据帧写入excel文件,这就是问题所在。我认为它之前没有出现的原因是因为我使用的是Jupyter笔记本,可能在它正确添加之前运行了一个单元格,所以我被误导了。谢谢你的帮助