简体   繁体   中英

Export multiple tables from Pandas read_html() to csv file

I figured out how to use pandas read_html() function to parse tables from an HTML file. But I'm having trouble outputting the tables to a single csv file.

This is what my tables look like in html, called 'example.html':

<html>

<body>


<table><tr></tr></table>
<table><tr></tr></table>

sjfasfasjfle
sdfølasjdf
fsdfjkj
fj
klsdjfølas
sdfpøjfjøklsdfasmf

Test1
<table border=1>
<tr>
<td>Trondheim</td><td>3</td><td>6</td><td>8.8</td><td>Test</td>
</tr>
<tr>
<td>4</td><td>7</td><td>8</td><td>88</td><td>Test</td>
</tr>
</table>

sdfjasøf<br>
aklsf<br>
klasdjfasljklj<br>
<p>
asdøfjs<br>
klasøflas<br>
øfsdjf<br><br>
kljøflsdjf<br>
kldfjølasjf<br>
<table><tr></tr></table>

Test2
<table border=1>
<tr>
<td>Norway</td><td>3</td><td>76</td><td>778.8</td><td>Test</td>
</tr>
<tr>
<td>74</td><td>77</td><td>78</td><td>88</td><td>Test</td>
</tr>
<tr>
<td>74</td><td>77</td><td>78</td><td>88</td><td>Test</td><td>74</td><td>77</td><td>78</td><td>88</td><td>Test</td>
</tr>
</table>


Test3
<table border=1><tr>Also</tr></table>

<table border=1><tr></tr></table>

<table border=1><tr></tr></table>

</body>

<html>

and this is my code to parse these tables:

import os
import pandas as pd

htmlname = r"example.html"
html = open(htmlname, 'r')
source_code = html.read()
tables = pd.read_html(source_code)

for i, table in enumerate(tables):
    tables.to_csv('test.csv','a')

I'm getting the error AttributeError: 'list' object has no attribute 'to_csv'.

I'm lost as to how to solve this and I'm pretty sure it's something easy but all my research has only taken me to examples with 1 table in an html file but not parsing multiple and writing multiple to a csv file. Any help is appreciated. Thank you in advanced.

You are writing tables to the csv instead of table try:

for i, table in enumerate(tables):
    table.to_csv('test{}.csv'.format(i),'a')

Also, is there any reason why you are using 'a' as a separator?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM