如标题所述,我想检查给定的文件对象(打开为二进制流)是有效的UTF-8文件。
任何?
由于
答案 0 :(得分:21)
def try_utf8(data):
"Returns a Unicode object on success, or None on failure"
try:
return data.decode('utf-8')
except UnicodeDecodeError:
return None
data = f.read()
udata = try_utf8(data)
if udata is None:
# Not UTF-8. Do something else
else:
# Handle unicode data
答案 1 :(得分:8)
您可以执行类似
的操作import codecs
try:
f = codecs.open(filename, encoding='utf-8', errors='strict')
for line in f:
pass
print "Valid utf-8"
except UnicodeDecodeError:
print "invalid utf-8"