DEV Community

Divyanshu Shekhar
Divyanshu Shekhar

Posted on

Python Web Scraping To CSV

Comma Separated Values or (CSV) is one of the most common formats of spreadsheet data storage files. This file extension is very popular in machine learning. Data used in machine learning are stored in CSV’s as working with CSV files in Python is easy with Pandas. CSV is also supported by Microsoft Excel, this is also a huge factor for the popularity of CSV file format.

Writing To CSV File In Python

First of all, we need to take a look at How to work with CSV Files in Python, in order to store our Python Web Scraping Data into it.

In this Example Section, where we will learn How to work with CSV in Python. We will save a CSV file at our workspace called test.csv that will contain three Columns and a total of 11 rows. Three Columns will be SR(Serial Number), ID(between 1-100), and Price(between 100-1000).

Here is the Python Code to Save a CSV File:

import csv
import random

csvFile = open('test.csv', 'w+')
try:
    writer = csv.writer(csvFile)
    writer.writerow(('SR', 'ID', 'Price'))
    for i in range(10):
        writer.writerow((i+1, random.randint(1, 100),
                         random.randint(100, 1000)))
finally:
    csvFile.close()
Enter fullscreen mode Exit fullscreen mode

To see the output and learn more about saving the python web scraping data to CSV from the original post.

Top comments (0)