Python Basics (37 Part Series)
1 Determining Python Version using the sys Library
2 Terminating a Python Program Using the Exit Function
… 33 more parts…
3 Python: A Guide to Variables
4 Python: Interacting with Users and Handling Sensitive Information
5 Python: Understanding Boolean Values and Operators
6 Introduction to the Python Math Library
7 Introduction to the Python Random Library
8 Working with Bytes in Python
9 Working with Tuples in Python
10 Working with Named Tuples in Python
11 Exploring Deques in Python
12 Python: A Guide to For and While Loops
13 Python: A Guide to Functions
14 Python: A Guide to Lambda Functions
15 Introduction to Classes in Python
16 Introduction to Python Modules and Libraries
17 Handling Exceptions in Python
18 Conditional Statements in Python
19 Disassembling Python Bytecode with the dis Library
20 Datetime Manipulation in Python
21 Using tomllib for Configuration in Python
22 Exploring the os Library With Python
23 Exploring API Requests in Python with the Official Joke API
24 Exploring Alice in Wonderland through Text Files in Python
25 Embracing the Zen of Python: A Guide to Pythonic Programming
26 Exploring the Antigravity Library in Python
27 Working with CSV Files in Python
28 Python Script Structure
29 Introduction to Logging in Python
30 Understanding Dataclasses in Python
31 Understanding the map Function in Python
32 Exploring (Some) Python Integrated Development Environments
33 Conversions in Python
34 Working with XlsxWriter in Python
35 Fortanix Library: Authentication and Security Object Retrieval
36 Interacting with SFTP Repositories: A Paramiko Integration
37 Exploring SharePoint with Microsoft Graph API
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 (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
<span># Reading the entire "Alice in Wonderland" text file at once </span><span>with</span> <span>open</span><span>(</span><span>file</span><span>=</span><span>"</span><span>alice.txt</span><span>"</span><span>,</span> <span>mode</span><span>=</span><span>"</span><span>r</span><span>"</span><span>,</span> <span>encoding</span><span>=</span><span>"</span><span>utf-8</span><span>"</span><span>)</span> <span>as</span> <span>file</span><span>:</span><span>alice_text</span> <span>=</span> <span>file</span><span>.</span><span>read</span><span>()</span><span>print</span><span>(</span><span>"</span><span>First 63 characters of Alice in Wonderland:</span><span>"</span><span>)</span><span>print</span><span>(</span><span>alice_text</span><span>[:</span><span>63</span><span>])</span><span># Reading the entire "Alice in Wonderland" text file at once </span><span>with</span> <span>open</span><span>(</span><span>file</span><span>=</span><span>"</span><span>alice.txt</span><span>"</span><span>,</span> <span>mode</span><span>=</span><span>"</span><span>r</span><span>"</span><span>,</span> <span>encoding</span><span>=</span><span>"</span><span>utf-8</span><span>"</span><span>)</span> <span>as</span> <span>file</span><span>:</span> <span>alice_text</span> <span>=</span> <span>file</span><span>.</span><span>read</span><span>()</span> <span>print</span><span>(</span><span>"</span><span>First 63 characters of Alice in Wonderland:</span><span>"</span><span>)</span> <span>print</span><span>(</span><span>alice_text</span><span>[:</span><span>63</span><span>])</span># 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])
Enter fullscreen mode Exit fullscreen mode
Output:
First 63 characters of Alice in Wonderland:The Project Gutenberg eBook of Alice's Adventures in WonderlandFirst 63 characters of Alice in Wonderland: The Project Gutenberg eBook of Alice's Adventures in WonderlandFirst 63 characters of Alice in Wonderland: The Project Gutenberg eBook of Alice's Adventures in Wonderland
Enter fullscreen mode Exit fullscreen mode
Reading “Alice in Wonderland” line by line
<span># Reading "Alice in Wonderland" line by line </span><span>with</span> <span>open</span><span>(</span><span>file</span><span>=</span><span>"</span><span>alice.txt</span><span>"</span><span>,</span> <span>mode</span><span>=</span><span>"</span><span>r</span><span>"</span><span>,</span> <span>encoding</span><span>=</span><span>"</span><span>utf-8</span><span>"</span><span>)</span> <span>as</span> <span>file</span><span>:</span><span>for</span> <span>line</span> <span>in</span> <span>file</span><span>:</span><span>print</span><span>(</span><span>line</span><span>.</span><span>strip</span><span>())</span> <span># Strip newline characters for cleaner output </span> <span># Process each line further if needed </span><span># Reading "Alice in Wonderland" line by line </span><span>with</span> <span>open</span><span>(</span><span>file</span><span>=</span><span>"</span><span>alice.txt</span><span>"</span><span>,</span> <span>mode</span><span>=</span><span>"</span><span>r</span><span>"</span><span>,</span> <span>encoding</span><span>=</span><span>"</span><span>utf-8</span><span>"</span><span>)</span> <span>as</span> <span>file</span><span>:</span> <span>for</span> <span>line</span> <span>in</span> <span>file</span><span>:</span> <span>print</span><span>(</span><span>line</span><span>.</span><span>strip</span><span>())</span> <span># Strip newline characters for cleaner output </span> <span># Process each line further if needed </span># 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
Enter fullscreen mode Exit fullscreen mode
Output:
The Project Gutenberg eBook of Alice's Adventures in Wonderland...The Project Gutenberg eBook of Alice's Adventures in Wonderland ...The Project Gutenberg eBook of Alice's Adventures in Wonderland ...
Enter fullscreen mode Exit fullscreen mode
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”
<span># Reading the entire "Alice in Wonderland" text file at once </span><span>with</span> <span>open</span><span>(</span><span>file</span><span>=</span><span>"</span><span>alice.txt</span><span>"</span><span>,</span> <span>mode</span><span>=</span><span>"</span><span>r</span><span>"</span><span>,</span> <span>encoding</span><span>=</span><span>"</span><span>utf-8</span><span>"</span><span>)</span> <span>as</span> <span>file</span><span>:</span><span>alice_text</span> <span>=</span> <span>file</span><span>.</span><span>read</span><span>()</span><span># Counting the number of words in "Alice in Wonderland" </span> <span>word_count</span> <span>=</span> <span>len</span><span>(</span><span>alice_text</span><span>.</span><span>split</span><span>())</span><span>print</span><span>(</span><span>"</span><span>Total words in Alice in Wonderland:</span><span>"</span><span>,</span> <span>word_count</span><span>)</span><span># Reading the entire "Alice in Wonderland" text file at once </span><span>with</span> <span>open</span><span>(</span><span>file</span><span>=</span><span>"</span><span>alice.txt</span><span>"</span><span>,</span> <span>mode</span><span>=</span><span>"</span><span>r</span><span>"</span><span>,</span> <span>encoding</span><span>=</span><span>"</span><span>utf-8</span><span>"</span><span>)</span> <span>as</span> <span>file</span><span>:</span> <span>alice_text</span> <span>=</span> <span>file</span><span>.</span><span>read</span><span>()</span> <span># Counting the number of words in "Alice in Wonderland" </span> <span>word_count</span> <span>=</span> <span>len</span><span>(</span><span>alice_text</span><span>.</span><span>split</span><span>())</span> <span>print</span><span>(</span><span>"</span><span>Total words in Alice in Wonderland:</span><span>"</span><span>,</span> <span>word_count</span><span>)</span># 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)
Enter fullscreen mode Exit fullscreen mode
Output:
Total words in Alice in Wonderland: 29564Total words in Alice in Wonderland: 29564Total words in Alice in Wonderland: 29564
Enter fullscreen mode Exit fullscreen mode
Extracting lines containing a specific word
<span># Extracting lines containing a specific word </span><span>target_word</span> <span>=</span> <span>"</span><span>rabbit</span><span>"</span><span>with</span> <span>open</span><span>(</span><span>file</span><span>=</span><span>"</span><span>alice.txt</span><span>"</span><span>,</span> <span>mode</span><span>=</span><span>"</span><span>r</span><span>"</span><span>,</span> <span>encoding</span><span>=</span><span>"</span><span>utf-8</span><span>"</span><span>)</span> <span>as</span> <span>file</span><span>:</span><span>lines_with_word</span> <span>=</span> <span>[</span><span>line</span><span>.</span><span>strip</span><span>()</span> <span>for</span> <span>line</span> <span>in</span> <span>file</span> <span>if</span> <span>target_word</span> <span>in</span> <span>line</span><span>.</span><span>lower</span><span>()]</span><span>print</span><span>(</span><span>"</span><span>Lines containing the word </span><span>'</span><span>rabbit</span><span>'</span><span>:</span><span>"</span><span>)</span><span>for</span> <span>line</span> <span>in</span> <span>lines_with_word</span><span>[:</span><span>3</span><span>]:</span> <span># Displaying the first 3 lines for brevity </span> <span>print</span><span>(</span><span>line</span><span>)</span><span># Extracting lines containing a specific word </span><span>target_word</span> <span>=</span> <span>"</span><span>rabbit</span><span>"</span> <span>with</span> <span>open</span><span>(</span><span>file</span><span>=</span><span>"</span><span>alice.txt</span><span>"</span><span>,</span> <span>mode</span><span>=</span><span>"</span><span>r</span><span>"</span><span>,</span> <span>encoding</span><span>=</span><span>"</span><span>utf-8</span><span>"</span><span>)</span> <span>as</span> <span>file</span><span>:</span> <span>lines_with_word</span> <span>=</span> <span>[</span><span>line</span><span>.</span><span>strip</span><span>()</span> <span>for</span> <span>line</span> <span>in</span> <span>file</span> <span>if</span> <span>target_word</span> <span>in</span> <span>line</span><span>.</span><span>lower</span><span>()]</span> <span>print</span><span>(</span><span>"</span><span>Lines containing the word </span><span>'</span><span>rabbit</span><span>'</span><span>:</span><span>"</span><span>)</span> <span>for</span> <span>line</span> <span>in</span> <span>lines_with_word</span><span>[:</span><span>3</span><span>]:</span> <span># Displaying the first 3 lines for brevity </span> <span>print</span><span>(</span><span>line</span><span>)</span># 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)
Enter fullscreen mode Exit fullscreen mode
Output:
Lines containing the word 'rabbit':CHAPTER I. Down the Rabbit-HoleCHAPTER IV. The Rabbit Sends in a Little BillDown the Rabbit-HoleLines containing the word 'rabbit': CHAPTER I. Down the Rabbit-Hole CHAPTER IV. The Rabbit Sends in a Little Bill Down the Rabbit-HoleLines containing the word 'rabbit': CHAPTER I. Down the Rabbit-Hole CHAPTER IV. The Rabbit Sends in a Little Bill Down the Rabbit-Hole
Enter fullscreen mode Exit fullscreen mode
Writing to Text Files
- Write annotations or processed data back to a text file.
Writing annotations to a new text file
<span># Writing annotations to a new text file </span><span>annotations</span> <span>=</span> <span>[</span><span>"</span><span>Chapter 1: Down the Rabbit Hole - Alice encounters the White Rabbit.</span><span>"</span><span>,</span><span>"</span><span>Chapter 2: The Pool of Tears - Alice meets the Mouse and the Dodo.</span><span>"</span><span>]</span><span>with</span> <span>open</span><span>(</span><span>file</span><span>=</span><span>"</span><span>alice_annotations.txt</span><span>"</span><span>,</span> <span>mode</span><span>=</span><span>"</span><span>w</span><span>"</span><span>,</span> <span>encoding</span><span>=</span><span>'</span><span>utf-8</span><span>'</span><span>)</span> <span>as</span> <span>file</span><span>:</span><span>for</span> <span>annotation</span> <span>in</span> <span>annotations</span><span>:</span><span>file</span><span>.</span><span>write</span><span>(</span><span>annotation</span> <span>+</span> <span>"</span><span>\n</span><span>"</span><span>)</span><span>print</span><span>(</span><span>"</span><span>Annotations written to alice_annotations.txt</span><span>"</span><span>)</span><span># Writing annotations to a new text file </span><span>annotations</span> <span>=</span> <span>[</span> <span>"</span><span>Chapter 1: Down the Rabbit Hole - Alice encounters the White Rabbit.</span><span>"</span><span>,</span> <span>"</span><span>Chapter 2: The Pool of Tears - Alice meets the Mouse and the Dodo.</span><span>"</span> <span>]</span> <span>with</span> <span>open</span><span>(</span><span>file</span><span>=</span><span>"</span><span>alice_annotations.txt</span><span>"</span><span>,</span> <span>mode</span><span>=</span><span>"</span><span>w</span><span>"</span><span>,</span> <span>encoding</span><span>=</span><span>'</span><span>utf-8</span><span>'</span><span>)</span> <span>as</span> <span>file</span><span>:</span> <span>for</span> <span>annotation</span> <span>in</span> <span>annotations</span><span>:</span> <span>file</span><span>.</span><span>write</span><span>(</span><span>annotation</span> <span>+</span> <span>"</span><span>\n</span><span>"</span><span>)</span> <span>print</span><span>(</span><span>"</span><span>Annotations written to alice_annotations.txt</span><span>"</span><span>)</span># 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")
Enter fullscreen mode Exit fullscreen mode
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.Chapter 1: Down the Rabbit Hole - Alice encounters the White Rabbit. Chapter 2: The Pool of Tears - Alice meets the Mouse and the Dodo.Chapter 1: Down the Rabbit Hole - Alice encounters the White Rabbit. Chapter 2: The Pool of Tears - Alice meets the Mouse and the Dodo.
Enter fullscreen mode Exit fullscreen mode
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.
Python Basics (37 Part Series)
1 Determining Python Version using the sys Library
2 Terminating a Python Program Using the Exit Function
… 33 more parts…
3 Python: A Guide to Variables
4 Python: Interacting with Users and Handling Sensitive Information
5 Python: Understanding Boolean Values and Operators
6 Introduction to the Python Math Library
7 Introduction to the Python Random Library
8 Working with Bytes in Python
9 Working with Tuples in Python
10 Working with Named Tuples in Python
11 Exploring Deques in Python
12 Python: A Guide to For and While Loops
13 Python: A Guide to Functions
14 Python: A Guide to Lambda Functions
15 Introduction to Classes in Python
16 Introduction to Python Modules and Libraries
17 Handling Exceptions in Python
18 Conditional Statements in Python
19 Disassembling Python Bytecode with the dis Library
20 Datetime Manipulation in Python
21 Using tomllib for Configuration in Python
22 Exploring the os Library With Python
23 Exploring API Requests in Python with the Official Joke API
24 Exploring Alice in Wonderland through Text Files in Python
25 Embracing the Zen of Python: A Guide to Pythonic Programming
26 Exploring the Antigravity Library in Python
27 Working with CSV Files in Python
28 Python Script Structure
29 Introduction to Logging in Python
30 Understanding Dataclasses in Python
31 Understanding the map Function in Python
32 Exploring (Some) Python Integrated Development Environments
33 Conversions in Python
34 Working with XlsxWriter in Python
35 Fortanix Library: Authentication and Security Object Retrieval
36 Interacting with SFTP Repositories: A Paramiko Integration
37 Exploring SharePoint with Microsoft Graph API
原文链接:Exploring Alice in Wonderland through Text Files in Python
暂无评论内容