Answer the question
In order to leave comments, you need to log in
How to read file in utf-8 in python?
Hello.
I create a text.txt file with utf-8 encoded text.
In Python I write:
f = open("text.txt", "r", encoding="utf-8")
print(f.read())
f.close()
Answer the question
In order to leave comments, you need to log in
So why delete the BOM symbol? Why extra manual work? Read with him:
import io
import chardet
import os
import codecs
filename = 'test_file.txt'
bytes = min(32, os.path.getsize(filename))
raw = open(filename, 'rb').read(bytes)
if raw.startswith(codecs.BOM_UTF8):
encoding = 'utf-8-sig'
else:
result = chardet.detect(raw)
encoding = result['encoding']
infile = io.open(filename, 'r', encoding=encoding)
data = infile.read()
infile.close()
print(data)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question