How to Transcode a File from Latin-1 to UTF-8 in Python
Read a latin1-encoded text file and rewrite it as UTF-8 using Python's pathlib and encoding parameters.
Python code
31 linesfrom pathlib import Path
def transcode_to_utf8(input_path, output_path):
"""Read a latin1-encoded file and write it as UTF-8."""
source = Path(input_path)
target = Path(output_path)
with source.open(encoding='latin1') as infile:
content = infile.read()
with target.open('w', encoding='utf-8') as outfile:
outfile.write(content)
return f"Transcoded '{input_path}' -> '{output_path}'"
if __name__ == "__main__":
# Create a sample latin1 file
sample = Path("sample_latin1.txt")
sample.write_bytes("Café naïve — 100%".encode('latin1'))
# Transcode it
result = transcode_to_utf8("sample_latin1.txt", "sample_utf8.txt")
print(result)
# Verify the output
output = Path("sample_utf8.txt").read_text(encoding='utf-8')
print(output)
# Cleanup example files
sample.unlink()
Path("sample_utf8.txt").unlink()
Output
Transcoded 'sample_latin1.txt' -> 'sample_utf8.txt'
Café naïve — 100%
How it works
The Path.open() method accepts an encoding parameter, so you can read the source with latin1 and write with utf-8. Reading the entire file into memory is fine for small to medium files; for huge files, process line by line. The encoding argument is passed directly to the underlying open(), so all Python-supported codecs work. Cleanup with unlink() removes the test files.
Common mistakes
- Using the wrong encoding name; latin1 is equivalent to iso-8859-1 and is case-insensitive
- Trying to write with `open` without specifying `encoding='utf-8'`, defaulting to locale
- Reading and writing the same file path, which can truncate the source while reading
Variations
- Use `codecs` module with `open()` for older Python versions, though not needed for 3.9+
- Iterate line by line with `for line in infile:` to handle large files
Real-world use cases
- Migrating legacy CSV exports stored in Windows-1252 or latin1 to UTF-8 before loading into a database.
- Converting old web page files from latin1 to UTF-8 for consistent metadata and international character support.
- Standardizing multiple source files with mixed encodings into UTF-8 in a data pipeline script.
Sponsored
More from Files & data
- Append a Line to a Log File in Python easy
- Audit File Permissions Across a Project in Python easy
- Automatically Detect Corrupted Files Using SHA-256 Checksums in Python easy
- Automatically Highlight Data Validation Errors Inside Excel Files in Python easy
- Build a Command-Line To-Do List Application with Data Persistence in Python easy
- Build a File Index by Relative Path Hash Map in Python easy
Keep learning
Related tutorials and quizzes for this topic.