**Working with Files, Databases, and Data Serialization

This lesson builds upon your existing Python knowledge by introducing file handling, database interaction, and data serialization techniques. You'll learn how to read from, write to, and manipulate files, interact with databases to store and retrieve data, and convert data into formats like JSON and CSV for easier sharing and processing.

Learning Objectives

  • Read, write, and manipulate data within text files.
  • Connect to and interact with a SQLite database using the `sqlite3` library.
  • Serialize and deserialize Python data structures using JSON format.
  • Understand and apply the basics of CSV file handling.

Text-to-Speech

Listen to the lesson content

Lesson Content

File Handling in Python

Python provides built-in functionalities to work with files. You can open files, read their content, write new content, and close them. The basic steps involve using the open() function to get a file object, and then methods like read(), write(), readline(), and close() to manipulate the file. Remember to always close files after you're done to release system resources.

Example: Reading from a file

# Create a dummy file called 'my_file.txt' with some text
with open('my_file.txt', 'w') as f:
    f.write('Hello, World!\nThis is a sample file.\nPython is awesome!')

with open('my_file.txt', 'r') as f:
    content = f.read()
    print(content)

Example: Writing to a file

with open('output.txt', 'w') as f:
    f.write('This is written to the file.\n')
    f.write('Another line of text.')

Modes of opening files:
* 'r' - Read mode (default)
* 'w' - Write mode (overwrites existing content)
* 'a' - Append mode (adds content to the end)
* 'x' - Create mode (creates the file if it doesn't exist, otherwise raises an error)
* 'r+' - Read and Write mode

Working with SQLite Databases

SQLite is a lightweight, file-based database that's perfect for smaller projects or for learning database concepts. Python's sqlite3 module makes interacting with SQLite databases straightforward.

Connecting to a Database:

import sqlite3

# Connect to the database (creates it if it doesn't exist)
conn = sqlite3.connect('mydatabase.db')

# Create a cursor object
cursor = conn.cursor()

Creating a Table:

cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY,
    name TEXT,
    email TEXT
)
''')

Inserting Data:

cursor.execute("INSERT INTO users (name, email) VALUES (?, ?)", ('Alice', 'alice@example.com'))
conn.commit()  # Save the changes

Querying Data:

cursor.execute("SELECT * FROM users")
results = cursor.fetchall()
print(results)

Closing the Connection:

conn.close()

Data Serialization with JSON

JSON (JavaScript Object Notation) is a widely used format for data exchange on the web. It's human-readable and easy to parse. Python's json module provides functions to encode Python objects into JSON strings (serialization) and decode JSON strings into Python objects (deserialization).

Encoding (Serialization):

import json

data = {
    'name': 'Bob',
    'age': 30,
    'city': 'New York',
    'skills': ['Python', 'SQL', 'Web Development']
}

json_string = json.dumps(data, indent=4)  # indent for readability
print(json_string)

Decoding (Deserialization):

import json

json_string = '{\n    "name": "Bob",\n    "age": 30,\n    "city": "New York",\n    "skills": ["Python", "SQL", "Web Development"]\n}'

data = json.loads(json_string)
print(data)
print(data['name'])

Writing to a JSON file:

import json

data = {
    'name': 'Charlie',
    'age': 25
}

with open('data.json', 'w') as f:
    json.dump(data, f, indent=4)

Reading from a JSON file:

import json

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

Working with CSV Files

CSV (Comma Separated Values) is another common format for data storage and exchange, particularly for tabular data. Python's csv module provides tools to read and write CSV files.

Writing to a CSV file:

import csv

data = [
    ['Name', 'Age', 'City'],
    ['David', 35, 'London'],
    ['Eve', 28, 'Paris']
]

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

Reading from a CSV file:

import csv

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

Using csv.DictWriter and csv.DictReader: These allow you to work with CSV data as dictionaries, which can be more convenient.

import csv

# Writing with DictWriter
data = [
    {'Name': 'David', 'Age': 35, 'City': 'London'},
    {'Name': 'Eve', 'Age': 28, 'City': 'Paris'}
]

with open('data_dict.csv', 'w', newline='') as file:
    fieldnames = ['Name', 'Age', 'City']
    writer = csv.DictWriter(file, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerows(data)

# Reading with DictReader
with open('data_dict.csv', 'r', newline='') as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(row)
Progress
0%