Convert HTML table into CSV file in python Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report CSV file is a Comma Separated Value file that uses a comma to separate values. CSV file is a useful thing in today's world when we are talking about machine learning, data handling, and data visualization. In this article, we will discuss how to convert an HTML table into a CSV file. Converting HTML Table into CSV file in Python Example: Suppose HTML file looks like, HTML table that can be converted to a CSV file using the BeautifulSoup and Pandas module of Python. These modules do not come built-in with Python. To install them type the below command in the terminal. pip install beautifulsoup4 pip install pandas Python3 Code for converting the HTML table into a CSV file Python3 # Importing the required modules import os import sys import pandas as pd from bs4 import BeautifulSoup path = 'html.html' # empty list data = [] # for getting the header from # the HTML file list_header = [] soup = BeautifulSoup(open(path),'html.parser') header = soup.find_all("table")[0].find("tr") for items in header: try: list_header.append(items.get_text()) except: continue # for getting the data HTML_data = soup.find_all("table")[0].find_all("tr")[1:] for element in HTML_data: sub_data = [] for sub_element in element: try: sub_data.append(sub_element.get_text()) except: continue data.append(sub_data) # Storing the data into Pandas # DataFrame dataFrame = pd.DataFrame(data = data, columns = list_header) # Converting Pandas DataFrame # into CSV file dataFrame.to_csv('Geeks.csv') Output: Comment More infoAdvertise with us Next Article Python | Convert an HTML table into excel S SohelRaja Follow Improve Article Tags : Technical Scripter Python Python-Library python-utility python-csv +1 More Practice Tags : python Similar Reads Convert CSV to HTML Table in Python CSV file is a Comma Separated Value file that uses a comma to separate values. It is basically used for exchanging data between different applications. In this, individual rows are separated by a newline. Fields of data in each row are delimited with a comma.Example :Â Â Name, Salary, Age, No.of year 2 min read Python | Convert an HTML table into excel MS Excel is a powerful tool for handling huge amounts of tabular data. It can be particularly useful for sorting, analyzing, performing complex calculations and visualizing data. In this article, we will discuss how to extract a table from a webpage and store it in Excel format. Step #1: Converting 2 min read Convert Excel to CSV in Python In this article, we will discuss how to convert an Excel (.xlsx) file to a CSV (.csv) file using Python. Excel files are commonly used to store data, and sometimes you may need to convert these files into CSV format for better compatibility or easier processing.Excel File Formats.xlsx: The newer Exc 3 min read Convert JSON to CSV in Python We can convert JSON to CSV in Python using the built-in json and csv modules.What is JSON and CSV?JSON is a lightweight, text-based data format commonly used to exchange data between web servers and clients. It stores data in key-value pairs and is typically used for data transmission in web applica 2 min read Convert multiple JSON files to CSV Python In this article, we will learn how to convert multiple JSON files to CSV file in Python. Before that just recall some terms : JSON File: Â A JSON file may be a file that stores simple data structures and objects in JavaScript Object Notation (JSON) format, which may be a standard data interchange for 8 min read Convert nested JSON to CSV in Python In this article, we will discuss how can we convert nested JSON to CSV in Python. An example of a simple JSON file: A simple JSON representationAs you can see in the example, a single key-value pair is separated by a colon (:) whereas each key-value pairs are separated by a comma (,). Here, "name", 9 min read Like