
The comma-separated values (CSV) is the most widely and convenient tabular data exchange format. Most CSV files originate from spreadsheet applications and relational database management systems (For example, MySQL). In a CSV file, commas separate field/column values, and linebreaks separate records/rows. The CSV format allows developers to store and transfer tabular data in plain-text files without using special software.
The Python programming language supports different libraries for reading and writing CSV files. Mostly, you require to parse CSV files with Python to automate tasks or analyze data. For instance, you can use Python to loop through large CSV files, filter data records, and insert the results in a relational database like MySQL or PostgreSQL.
This guide describes how to write and read CSV files with Python on Ubuntu 20.04 server.
To follow along with this guide:
Before you start reading CSV files with Python, you need a project directory and a sample CSV file. Follow the steps below to carry out the above tasks:
Create a new project directory.
Switch to the new project directory.
Open a new products.csv file in a text editor. In a production environment, the CSV file might come from spreadsheet applications or databases like PostgreSQL or MySQL.
Enter the following CSV data records into the products.csv file. In the following file, separate field/column values with commas (,) and put each record on a separate line.
Save and close the products.csv file.
To manipulate CSV files with Python, you require the csv module. The module contains the following functions for reading and writing CSV data:
reader(): This function loops a given CSV file and returns data rows as a list of strings.writer(): This function converts raw input data from Python into a CSV delimited string for storage.To read data from the sample products.csv file that you created earlier, follow the steps below:
Open a new read_csv.py file in a text editor.
Enter the following information into the read_csv.py file.
Save and close the read_csv.py file.
Check out the read_csv.py source code explanation below.
The import csv declaration at the top loads the Python csv module.
The fileName = 'products.csv' statement declares the CSV path.
The with open(fileName, 'r') as csvFile: line opens the products.csv file. The r parameter declares the read mode.
The dataRows = csv.reader(csvFile) statement reads the products.csv file and returns a list of records.
The for dataRow in dataRows: statement loops through the data records in the dataRows object.
The print(dataRow) statement outputs each row's data.
Execute the read_csv.py file using the following command.
The read_csv.py script displays the following output.
In this guide, you only have a few sample records in the CSV file. In a production environment, you might have thousands of records. To implement a paging mechanism that allows you to retrieve only a few records at a time, follow the steps below:
Review the syntax below that allows you to convert the CSV reader object (dataRows = csv.reader(csvFile)) into a Python list.
Understand how to use the square brackets and Python's slicing syntax to access the data records using a list's start and stop indices.
To read the first four records from the products.csv file, delete the existing read_csv.py file and create a new file.
Enter the following information into the read_csv.py file.
Save and close the read_csv.py file.
Run the read_csv.py file.
Output.
Recreate the read_csv.py again.
Change the read_csv.py file to retrieve the next records in the list as follows.
Run the read_csv.py file.
Output.
The csv.writer() function discussed earlier allows you to write data into CSV files. Follow the steps below to use the module.
Open a new write_csv.py file in a text editor.
Enter the following information into the write_csv.py file.
Save and close the write_csv.py file.
Review the write_csv.py file source code.
The import csv declaration at the top loads the Python csv module.
The row6 = [...], row7 = [...], and row8 = [...] statements initialize Python lists objects containing comma-separated values for each additional row that you want to add to the products.csv file.
The fileName = 'products.csv' statement defines the path.
The with open(fileName, 'a') as csvFile: function opens the products.csv file. The `a' parameter instructs the function to open the file in append mode.
The writer = csv.writer(csvFile) statement returns an object for writing data into the products.csv file.
The writer.writerow(...) functions write row6, row7, and row8 into the products.csv file.
The print("Success") line displays a Success message when the code executes.
Run the write_csv.py file.
Output.
Recreate the read_csv.py file.
Enter the following information into the read_csv.py file to read all the records from the products.csv file.
Save and close the read_csv.py file.
Execute the read_csv.py to check whether the write_csv.py script has successfully updated the products.csv file.
Output.
This guide shows you how to work with CSV files using the Python csv module. The sample Python code in the guide use the csv.reader() and csv.writer() functions to parse and append data into CSV files. Then, the Python list syntax describes how to page CSV rows when working with large files.
0 Comments
Be the first to comment and share your perspective with the community.