Categories
Python Answers

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

Spread the love

Sometimes, we want to read and write to Unicode (UTF-8) files in Python.

In this article, we’ll look at how to read and write to Unicode (UTF-8) files in Python.

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

To read and write to Unicode (UTF-8) files in Python, we can use the io.open function.

For instance, we write

import io

f = io.open("test", mode="r", encoding="utf-8")

to call io.open with the path of the file to open, the permission mode and the encoding of the file.

We set the encoding to 'utf-8' to read the file as a Unicode file.

Conclusion

To read and write to Unicode (UTF-8) files in Python, we can use the io.open function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *