Introduction
In this lesson, you'll expand your Python skills by learning how to handle two critical data formats: CSV (Comma-Separated Values) and JSON (JavaScript Object Notation). These formats are widely used for data interchange across various applications due to their simplicity and interoperability. We'll explore how to read from, process, and write to CSV and JSON files using Python, utilizing standard libraries that provide robust features for handling these data types.
Working with CSV Files
CSV files are a straightforward format that can easily represent tabular data. Each line in a CSV file is a data record, with each record consisting of one or more fields.
Reading CSV Files
Python's csv module offers convenient methods for reading and writing CSV files.
import csv
with open('example.csv', mode='r') as file:
csv_reader = csv.reader(file)
for row in csv_reader:
print(row)
In this example, csv.reader reads the file contents and returns each row as a list.
Writing to CSV Files
You can also write data to CSV files using the csv module.
import csv
data = [['Name', 'Age', 'City'], ['Alice', '30', 'New York'], ['Bob', '25', 'Los Angeles']]
with open('output.csv', mode='w', newline='') as file:
csv_writer = csv.writer(file)
csv_writer.writerows(data)
By using csv.writer, you can write multiple rows efficiently to the output file.
Introduction to JSON Format
JSON is a lightweight data-interchange format that is easy to read and write for humans and machines alike. Its text-based structure, consisting of key-value pairs, is ideal for data representation.
Parsing JSON Files
The json module in Python is well-equipped for parsing JSON data.
import json
with open('example.json', 'r') as file:
data = json.load(file)
print(data)
Using json.load, you can directly convert JSON data from a file into Python dictionaries, which allows for easy data manipulation.
Writing to JSON Files
Writing data back to a JSON file is straightforward with json.dump.
import json
data = {'Name': 'Alice', 'Age': 30, 'City': 'New York'}
with open('output.json', 'w') as file:
json.dump(data, file, indent=4)
The indent parameter makes the output more readable by adding indentation.
Practical Examples
Combining CSV and JSON
A common requirement is converting CSV data into JSON for web applications. This requires reading data from a CSV, processing it in Python, and then writing the data as JSON.
import csv
import json
csv_file = 'example.csv'
json_file = 'converted.json'
data = {}
# Read CSV
with open(csv_file) as file:
csv_reader = csv.DictReader(file)
for rows in csv_reader:
key = rows['Name']
data[key] = rows
# Write JSON
with open(json_file, 'w') as file:
json.dump(data, file, indent=4)
This script reads records from a CSV file and converts them into a dictionary, with the 'Name' field as keys, before saving them to a JSON file.
Summary / Key Takeaways
In this lesson Working with CSV and JSON Files, you have learned:
- CSVs are text files that store tabular data, easily read and written using Python's
csvmodule. - JSON is a flexible data format ideal for web applications, easily parsed with the
jsonmodule. - Use
csv.readerandcsv.writerfor handling CSV files, andjson.loadandjson.dumpfor JSON. - Python's dictionary structure complements JSON parsing perfectly, facilitating easy manipulation of JSON data.
- Practical data conversion between CSV and JSON demonstrates Python's powerful file handling capabilities.
