3.6 Working with CSV and JSON

3.6 Working with CSV and JSON

In this section, we will explore how to work with CSV (Comma-Separated Values) and JSON (JavaScript Object Notation) files in Python. These file formats are commonly used for storing and exchanging data, and Python provides built-in libraries to handle them efficiently.

Working with CSV Files

CSV files are a simple and widely used format for storing tabular data. They consist of rows and columns, with each row representing a record and each column representing a field or attribute. Python provides the csv module to read from and write to CSV files.

To work with CSV files, we first need to import the csv module:

import csv

Reading CSV Files

To read data from a CSV file, we can use the reader object from the csv module. The reader object allows us to iterate over the rows of the CSV file.

Here's an example of reading data from a CSV file named "data.csv":

import csv

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

In the above code, we open the CSV file using the open() function and pass the file object to the csv.reader() function. We then iterate over the rows using a for loop and print each row.

Writing to CSV Files

To write data to a CSV file, we can use the writer object from the csv module. The writer object allows us to write rows to the CSV file.

Here's an example of writing data to a CSV file named "output.csv":

import csv

data = [
    ['Name', 'Age', 'Country'],
    ['John', '25', 'USA'],
    ['Alice', '30', 'Canada'],
    ['Bob', '35', 'UK']
]

with open('output.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(data)

In the above code, we define a list of lists data where each inner list represents a row of data. We then open the CSV file using the open() function with the mode set to 'w' (write) and pass the file object to the csv.writer() function. Finally, we use the writerows() method to write all the rows to the CSV file.

Working with JSON Files

JSON is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. Python provides the json module to work with JSON files.

To work with JSON files, we first need to import the json module:

import json

Reading JSON Files

To read data from a JSON file, we can use the load() function from the json module. The load() function allows us to load the JSON data from a file into a Python object.

Here's an example of reading data from a JSON file named "data.json":

import json

with open('data.json', 'r') as file:
    data = json.load(file)
    print(data)

In the above code, we open the JSON file using the open() function and pass the file object to the json.load() function. The json.load() function reads the JSON data from the file and converts it into a Python object. We can then access and manipulate the data as needed.

Writing to JSON Files

To write data to a JSON file, we can use the dump() function from the json module. The dump() function allows us to write the JSON data from a Python object to a file.

Here's an example of writing data to a JSON file named "output.json":

import json

data = {
    'name': 'John',
    'age': 25,
    'country': 'USA'
}

with open('output.json', 'w') as file:
    json.dump(data, file)

In the above code, we define a Python dictionary data that represents the JSON data. We then open the JSON file using the open() function with the mode set to 'w' (write) and pass the file object to the json.dump() function. The json.dump() function converts the Python object into JSON data and writes it to the file.

Conclusion

Working with CSV and JSON files is essential for handling data in Python. The csv module provides convenient functions for reading and writing CSV files, while the json module allows us to work with JSON files seamlessly. By mastering these techniques, you will be able to efficiently process and manipulate data stored in these popular file formats.