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.

Easy Python 3.9+ Aug 9, 2026 Files & data 13 views 0 copies

Python code

31 lines
Python 3.9+
from 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

stdout
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

  1. Use `codecs` module with `open()` for older Python versions, though not needed for 3.9+
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Files & data

Related tutorials and quizzes for this topic.