4.1 Understanding Read Functions
In Python, files can be read using built-in functions. The most commonly used methods are read()
, readline()
, and readlines()
.
read()
: Reads the entire content of the file as a string.pythonwith open('example.txt', 'r') as file: content = file.read() print(content)
readline()
: Reads one line at a time from the file.pythonwith open('example.txt', 'r') as file: line = file.readline() print(line)
readlines()
: Reads all lines into a list.pythonwith open('example.txt', 'r') as file: lines = file.readlines() print(lines)
4.2 Understanding Write Functions
Writing to a file in Python is done using write()
and writelines()
methods.
write()
: Writes a string to the file.pythonwith open('example.txt', 'w') as file: file.write("Hello, World!")
writelines()
: Writes a list of strings to the file.pythonwith open('example.txt', 'w') as file: lines = ["Line 1\n", "Line 2\n"] file.writelines(lines)
Files can be opened in different modes:
'r'
: Read mode (default).'w'
: Write mode (overwrites the file).'a'
: Append mode (adds to the end of the file).
4.3 Programming Using File Operations
To demonstrate the power of file I/O in programming, here’s a simple program that reads from one file, processes the data, and writes the output to another file.
Example: Copy content from one file to another.
python
with open('input.txt', 'r') as infile, open('output.txt', 'w') as outfile:
content = infile.read()
outfile.write(content)
You can also combine file operations with conditionals and loops for more complex tasks, such as reading a file line-by-line, processing the content, and saving the result.
4.4 Powerful Pattern Matching and Searching
Text processing often requires searching for specific patterns in data. Python provides tools like regular expressions (regex) to search and manipulate strings based on patterns.
Example: Search for a word in a string.
python
import re
text = "Python is fun!"
pattern = r"Python"
if re.search(pattern, text):
print("Pattern found!")
The re.search()
function searches for the pattern in the text and returns a match object if the pattern is found.
4.5 Power of Pattern Searching Using Regex
Regular expressions (regex) allow for advanced pattern matching and searching. Some common operations include:
- Matching a specific pattern:
re.match()
- Finding all occurrences:
re.findall()
- Replacing text based on a pattern:
re.sub()
Example: Extract all email addresses from a string.
python
import re
text = "Contact us at info@example.com or support@domain.com."
emails = re.findall(r'\S+@\S+', text)
print(emails)
Common Regex Patterns:
\d
: Matches any digit.\w
: Matches any alphanumeric character.+
: Matches one or more repetitions of the previous character.*
: Matches zero or more repetitions of the previous character.
Example: Replace all digits with #
.
python
import re
text = "Phone number: 123-456-7890"
new_text = re.sub(r'\d', '#', text)
print(new_text) # Output: Phone number: ###-###-####
Regex is a powerful tool for text processing, allowing you to perform complex searches, replacements, and pattern matching.
Summary:
- File I/O allows you to read and write data to files using functions like
read()
,write()
, andwritelines()
. - Text Processing can be enhanced with regular expressions, making it easy to search and manipulate strings.
- Regex provides a powerful way to match patterns in text, enabling efficient searching and data extraction.
0 Comments