Welcome to Day 19 of our Python journey! Today, we're exploring the world of data manipulation as we explore the complexities of working with JSON and CSV files in Python. In the realm of data processing, these two file formats play pivotal roles, serving as the backbone for storing and exchanging structured data across a wide range of applications.
CSV
CSV (Comma-Separated Values) files have long been a staple in data handling, particularly for storing tabular data in a plain text format. CSV files are a popular choice for storing tabular data in a human-readable format. Python's built-in csv
module provides robust functionality for working with CSV files, allowing seamless integration into Python programs.
Reading CSV Files in Python: To read data from a CSV file in Python, we typically use the csv.reader
class. This class enables us to iterate over the rows of the CSV file and access the data elements within each row.
import csv
with open('animals.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
Writing to CSV Files in Python: Writing data to a CSV file in Python is straightforward with the csv.writer
class. We can create a writer object and use the writerow()
method to write rows of data to the CSV file.
import csv
animal_data = [
['Animal','Name'],
['Deer', 'Bambi'],
['Rabbit', 'Thumper']
]
with open('animals_output.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(animal_data)
JSON
JSON (JavaScript Object Notation) has emerged as a lightweight and versatile format for representing data in a human-readable and easily parsable form. Its simplicity and flexibility make it a preferred choice for transmitting data between web servers and clients, storing configuration settings, and structuring complex data objects.
When examining the JSON format, it resembles a collection of dictionaries. However, the distinction lies in JSON being represented as a string of text. Thus, before writing to a file, we must convert our Python objects into text format, a functionality provided by the json library.
Handling JSON Files in Python:
JSON files are commonly used for storing structured data in a lightweight and readable format. Python's built-in json
module provides functions for working with JSON data seamlessly.
import json
data = {
"Animals": {
"Deer": "Bambi",
"Rabbit": "Thumper",
}
}
json_file = open("animals.json", 'w')
json.dump(data, json_file)
json_file.close()
The above code will make a JSON file of the name 'animals.json' and write the data to it.
import json
animal_data = {
"Animals": [
{"animal": "Deer", "name": "Bambi"},
{"animal": "Rabbit", "name": "Thumper"}
]
}
#writing to JSON file
with open("animals.json", "w") as file:
json.dump(animal_data, file)
#reading from JSON file
with open("animals.json", "r") as file:
data = json.load(file)
print(data)
This code demonstrates how to write data to a JSON file in Python using the json
module.
Firstly, we import the json
module to access its functionalities for handling JSON data. Next, we define our sample data data
as a dictionary containing animal names as keys and corresponding characters as values.
We then open the file "animals.json" in write mode using the open()
function, indicating our intention to write to the file. We use the json.dump()
function to serialize the data
dictionary and write it to the JSON file json_file
.