File I/O Techniques
Reading Files with Encoding
Basic File Reading
Python provides multiple methods to read files with specific encodings:
## Reading a text file with UTF-8 encoding
with open('example.txt', 'r', encoding='utf-8') as file:
content = file.read()
print(content)
Reading Large Files
For large files, use iterative reading techniques:
## Reading file line by line
with open('large_file.txt', 'r', encoding='utf-8') as file:
for line in file:
print(line.strip())
Writing Files with Encoding
Writing Text Files
## Writing files with specific encoding
with open('output.txt', 'w', encoding='utf-8') as file:
file.write("Python encoding demonstration")
Encoding Conversion Techniques
graph LR
A[Source Encoding] --> B[Decode]
B --> C[Unicode]
C --> D[Encode]
D --> E[Target Encoding]
Conversion Example
## Converting between encodings
def convert_encoding(input_file, output_file, input_encoding, output_encoding):
with open(input_file, 'r', encoding=input_encoding) as infile:
content = infile.read()
with open(output_file, 'w', encoding=output_encoding) as outfile:
outfile.write(content)
Handling Encoding Errors
Error Handling Method |
Description |
'strict' |
Raises UnicodeError |
'ignore' |
Skips problematic characters |
'replace' |
Replaces with replacement character |
Error Handling Example
## Handling encoding errors
with open('problematic_file.txt', 'r', encoding='utf-8', errors='replace') as file:
content = file.read()
Advanced File Encoding Techniques
Binary File Handling
## Reading binary files
with open('binary_file.bin', 'rb') as file:
binary_content = file.read()
- Use buffered reading for large files
- Choose appropriate encoding based on data source
- Handle potential encoding exceptions
LabEx Encoding Best Practices
- Always specify encoding explicitly
- Use UTF-8 as default encoding
- Implement robust error handling
- Understand source data characteristics
By mastering these file I/O techniques, LabEx learners can effectively manage text encoding in various Python projects.