2010-07-16 91 views

回答

6

你可以做类似

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" 
+0

是担任一个魅力对我来说!谢谢 – Somar 2017-02-27 12:34:39

18
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 
+0

很明显,我没有做足够好的功课,当有更多的解决方案简单,因为这:( 谢谢! – Jox 2010-07-16 23:53:07