Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to search and replace text in a file using Python?
File manipulation is a fundamental aspect of programming, especially when dealing with data processing and management. Python offers powerful tools for handling files and text efficiently.
A common task is searching for specific text patterns within a file and replacing them with desired content. This article explores several practical methods for searching and replacing text in a file using Python.
Basic Text Replacement
Let's start with a simple example of searching for a specific word in a file and replacing it with another word. We'll search for the word "old" and replace it with "new" ?
def search_and_replace(file_path, search_word, replace_word):
try:
with open(file_path, 'r') as file:
file_contents = file.read()
updated_contents = file_contents.replace(search_word, replace_word)
with open(file_path, 'w') as file:
file.write(updated_contents)
print("Text replacement completed successfully.")
except FileNotFoundError:
print(f"Error: The file '{file_path}' was not found.")
except Exception as e:
print(f"An error occurred: {e}")
# Create a sample file
file_path = 'example.txt'
with open(file_path, 'w') as f:
f.write("This is an old house. An old car is parked there.")
# Perform replacement
search_and_replace(file_path, 'old', 'new')
# Read and display the result
with open(file_path, 'r') as f:
print("Updated content:", f.read())
Text replacement completed successfully. Updated content: This is an new house. An new car is parked there.
Case-Insensitive Text Replacement
Sometimes we need to perform case-insensitive search and replace operations. We can use regular expressions with the re module for this purpose ?
import re
def case_insensitive_search_and_replace(file_path, search_word, replace_word):
with open(file_path, 'r') as file:
file_contents = file.read()
pattern = re.compile(re.escape(search_word), re.IGNORECASE)
updated_contents = pattern.sub(replace_word, file_contents)
with open(file_path, 'w') as file:
file.write(updated_contents)
return updated_contents
# Create sample file with mixed case
with open('example.txt', 'w') as f:
f.write("This is an OLD file. It has some old content. And some Old stuff.")
result = case_insensitive_search_and_replace('example.txt', 'old', 'new')
print("Result:", result)
Result: This is an new file. It has some new content. And some new stuff.
Regular Expression Search and Replace
Regular expressions provide powerful pattern matching capabilities for complex search and replace operations. Here's an example that finds numbers and wraps them in brackets ?
import re
def regex_search_and_replace(file_path, search_pattern, replace_pattern):
with open(file_path, 'r') as file:
file_contents = file.read()
updated_contents = re.sub(search_pattern, replace_pattern, file_contents)
with open(file_path, 'w') as file:
file.write(updated_contents)
return updated_contents
# Create sample file with numbers
with open('example.txt', 'w') as f:
f.write("This is a test file with numbers like 123, 45, and 6.")
# Replace numbers with bracketed versions
result = regex_search_and_replace('example.txt', r'\b(\d+)\b', r'[\1]')
print("Result:", result)
Result: This is a test file with numbers like [123], [45], and [6].
Context-Aware Text Replacement
Sometimes we need to preserve the original case when replacing text. Here's a more sophisticated approach that maintains capitalization patterns ?
import re
def preserve_case_replacement(file_path, search_word, replace_word):
with open(file_path, 'r') as file:
file_contents = file.read()
def replacement_func(match):
original = match.group()
if original.isupper():
return replace_word.upper()
elif original.istitle():
return replace_word.capitalize()
else:
return replace_word.lower()
pattern = re.compile(rf'\b{re.escape(search_word)}\b', re.IGNORECASE)
updated_contents = pattern.sub(replacement_func, file_contents)
with open(file_path, 'w') as file:
file.write(updated_contents)
return updated_contents
# Create sample file with different cases
with open('example.txt', 'w') as f:
f.write("This is an old file. It contains OLD data. The word Old appears here.")
result = preserve_case_replacement('example.txt', 'old', 'new')
print("Result:", result)
Result: This is an new file. It contains NEW data. The word New appears here.
Comparison of Methods
| Method | Use Case | Complexity | Features |
|---|---|---|---|
| Basic replace() | Simple exact matches | Low | Fast, case-sensitive |
| Case-insensitive regex | Ignore case differences | Medium | Flexible matching |
| Advanced regex | Pattern-based replacement | High | Complex patterns, groups |
| Context-aware | Preserve formatting | High | Case preservation |
Conclusion
Python provides multiple approaches for searching and replacing text in files. Use basic replace() for simple substitutions, regular expressions for pattern matching, and context-aware methods when preserving formatting is important.
