Writing Unicode text to a text file?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python, writing Unicode text to a file is usually simple: open the file in text mode and specify an encoding such as UTF-8. The main problems come from relying on platform defaults, mixing text and bytes APIs, or reading the file later with a different encoding than the one used to write it.
Open the file in text mode with an explicit encoding
The safest default is UTF-8:
That is the core answer. Python will encode the Unicode string into UTF-8 bytes before writing it to disk.
Using an explicit encoding matters because default encodings vary across platforms and environments. A script that works on one machine may produce mojibake or raise errors on another if the encoding is left implicit.
Read it back with the same encoding
Writing succeeds only half the time if later code reads the file with the wrong encoding. Pair the read side with the same explicit encoding:
This is especially important in command-line tools, data pipelines, and test fixtures where files may be opened on different machines.
Understand the difference between text and bytes
Python text files expect str, not raw bytes. If you already have bytes, open the file in binary mode instead:
Likewise, do not manually encode a string and then pass it to a text-mode file object. That mixes the two layers incorrectly.
The practical rule is:
- use text mode plus
encoding=when working withstr - use binary mode when working with
bytes
pathlib works the same way
If you prefer pathlib, the same rule applies:
This can be cleaner in scripts that already use Path objects for filesystem work.
Handle replacement and error strategies deliberately
Sometimes you are not sure whether all characters can be represented in the target encoding. In that case, Python lets you choose an error strategy:
That will replace characters ASCII cannot represent. Usually, though, the better answer is not to degrade the data at all. It is to choose UTF-8 unless you have a real external constraint.
Know when a BOM matters
UTF-8 usually does not need a byte order mark. But some older Windows tools expect one. If you must interoperate with such tools, Python provides utf-8-sig:
That is not the normal default for modern systems, but it can be useful when exchanging files with legacy software.
Common Pitfalls
The most common mistake is writing Unicode text without specifying encoding, then being surprised when the file looks wrong on another machine.
Another common issue is mixing text and binary APIs, such as encoding a string manually and then writing it through a text-mode file.
People also forget that reading must use the same encoding assumptions as writing. A correctly written UTF-8 file can still look broken if it is read as something else.
Finally, if a downstream system requires ASCII or another restricted encoding, do not ignore the data-loss implications. Decide whether replacement, escaping, or a different transport format is acceptable.
Summary
- In Python, write Unicode text with
open(..., encoding="utf-8"). - Read it back with the same explicit encoding.
- Use text mode for
strand binary mode forbytes. - Prefer UTF-8 unless you have a specific compatibility constraint.
- Be deliberate about error handling and BOM usage when interoperating with older tools.
Related reading
- Writing unit tests in Python How do I start?
- WSGI vs ASGI server with hybrid sync/async Django app
- XGBModel' object has no attribute 'evals_result_
- XGBoost AttributeError 'DataFrame' object has no attribute 'feature_names
- xgboost AttributeError 'DMatrix' object has no attribute 'handle
- xlrd.biffh.XLRDError Excel xlsx file; not supported
- You are trying to add a non-nullable field 'new_field' to userprofile without a default
- You must feed a value for placeholder tensor 'Placeholder' with dtype float
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.