Exploring Alice in Wonderland through Text Files in Python
Tags: python, programming, beginners
Published: 2024-02-26 22:54:39

# Introduction

In this chapter, we'll delve into the enchanting world of "Alice in Wonderland" by Lewis Carroll using Python. We'll learn how to read text files, explore different approaches for processing text data, and even write our annotations back to a text file.

Book [Alice's Adventures in Wonderland by Lewis Carroll](https://www.gutenberg.org/ebooks/11) (Plain Text UTF-8).

# Topics

- Reading Text Files
- Processing Text Data
- Writing to Text Files

# Reading Text Files

- Read text files line by line or all at once.
- Handle text file encoding for proper interpretation.

## Reading the entire "Alice in Wonderland" text file at once

```python
# Reading the entire "Alice in Wonderland" text file at once
with open(file="alice.txt", mode="r", encoding="utf-8") as file:
    alice_text = file.read()

print("First 63 characters of Alice in Wonderland:")
print(alice_text[:63])
```

Output:

```
First 63 characters of Alice in Wonderland:
The Project Gutenberg eBook of Alice's Adventures in Wonderland
```

## Reading "Alice in Wonderland" line by line

```python
# Reading "Alice in Wonderland" line by line
with open(file="alice.txt", mode="r", encoding="utf-8") as file:
    for line in file:
        print(line.strip())  # Strip newline characters for cleaner output
        # Process each line further if needed
```

Output:

```
The Project Gutenberg eBook of Alice's Adventures in Wonderland
...
```

# Processing Text Data

- Analyze the text data for insights or perform operations such as counting words or extracting specific information.

## Counting the number of words in "Alice in Wonderland"

```python
# Reading the entire "Alice in Wonderland" text file at once
with open(file="alice.txt", mode="r", encoding="utf-8") as file:
    alice_text = file.read()

    # Counting the number of words in "Alice in Wonderland"
    word_count = len(alice_text.split())
    print("Total words in Alice in Wonderland:", word_count)
```

Output:

```
Total words in Alice in Wonderland: 29564
```

## Extracting lines containing a specific word

```python
# Extracting lines containing a specific word
target_word = "rabbit"
with open(file="alice.txt", mode="r", encoding="utf-8") as file:
    lines_with_word = [line.strip() for line in file if target_word in line.lower()]

print("Lines containing the word 'rabbit':")
for line in lines_with_word[:3]:  # Displaying the first 3 lines for brevity
    print(line)
```

Output:

```
Lines containing the word 'rabbit':
CHAPTER I.     Down the Rabbit-Hole
CHAPTER IV.    The Rabbit Sends in a Little Bill
Down the Rabbit-Hole
```

# Writing to Text Files

- Write annotations or processed data back to a text file.

## Writing annotations to a new text file

```python
# Writing annotations to a new text file
annotations = [
    "Chapter 1: Down the Rabbit Hole - Alice encounters the White Rabbit.",
    "Chapter 2: The Pool of Tears - Alice meets the Mouse and the Dodo."
]

with open(file="alice_annotations.txt", mode="w", encoding='utf-8') as file:
    for annotation in annotations:
        file.write(annotation + "\n")

print("Annotations written to alice_annotations.txt")
```

Output file alice_annotations.txt:

```
Chapter 1: Down the Rabbit Hole - Alice encounters the White Rabbit.
Chapter 2: The Pool of Tears - Alice meets the Mouse and the Dodo.
```

# Conclusion

Reading and processing text files in Python opens up possibilities for exploring literary works like "Alice in Wonderland". By mastering techniques such as reading files line by line, handling encoding issues, and writing data back to files, you can analyze, annotate, and interact with text data meaningfully. Whether diving into classic literature or analyzing contemporary texts, Python provides powerful tools for text file manipulation and exploration.