简体   繁体   中英

Converting HTML table to CSV file using python

I am very new to pandas, so I wanted to convert this HTML table to CSV file with the pandas however my CSV file is giving me a weird sign and it didn't manage to covert all the table over to the CSV.
Here's my code. I read about using beautifulsoup but I'm not too sure how to use the function.

import as pandas
df = pd.read_html('https://aim-sg.caas.gov.sg/aip/2020-10-13/final/2020-09-10-Non-AIR'
              'AC/html/eAIP/ENR-3.1-en-GB.html?s=B2EE1C5E1D2A684224A194E69D18338A560504FC#ENR-3.1')
df[0].to_csv('ENR3.0.csv')

Thank you!

Edited: I have changed my import to import pandas as dp but i still did not manage to convert all the HTML table to CSV file.

Greatly appreciate all your help!

You can use pandas itself to do this. You have messed up with the import statement. Here is how you do it correctly:

import pandas as pd
df = pd.read_html('https://aim-sg.caas.gov.sg/aip/2020-10-13/final/2020-09-10-Non-AIR'
              'AC/html/eAIP/ENR-3.1-en-GB.html?s=B2EE1C5E1D2A684224A194E69D18338A560504FC#ENR-3.1')

df[0].to_csv('ENR3.0.csv', index = False)

If you want to get all the dataframes present within the variable df , then replace the last line with this:

for x in range(len(df)):
    df[x].to_csv(f"CSV_File_{x+1}", index = False)

There is issue in import statement
It should be import pandas as pd and not import as pandas , as your are using alias pd in the code below.

Study about beautiful soup and use lxml parser to parse required data ( it is very fast ).
This link might help you out:
BeautifulSoup different parsers


If any other help is required, then do leave a comment on this post and will try to sort our your issue :)



Made correction in your code:

 import pandas as pd df = pd.read_html('https://aim-sg.caas.gov.sg/aip/2020-10-13/final/2020-09-10-Non-AIR' 'AC/html/eAIP/ENR-3.1-en-GB.html?s=B2EE1C5E1D2A684224A194E69D18338A560504FC#ENR-3.1') df[0].to_csv('ENR3.0.csv')

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