How to read and write unicode (UTF-8) files in Python?


The io module is now recommended and is compatible with Python 3's open syntax: The following code is used to read and write to unicode(UTF-8) files in Python

Example

import io
with io.open(filename,'r',encoding='utf8') as f:
    text = f.read()
# process Unicode text
with io.open(filename,'w',encoding='utf8') as f:
    f.write(text)

Updated on: 20-Feb-2020

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements