以下Python脚本适用于Python 2.3和Python 2.4(没有内置定义all():
#! /usr/bin/env python
# vim: set fileencoding=utf-8
# (c) Uwe Kleine-König
# GPLv2
import locale
import sys
f = file(sys.argv[1])
data = f.read()
def len_utf8_char(data):
if not 'all' in dir(__builtins__):
def all(seq):
for i in seq:
if not i:
return False
return True
def check_cont(num):
if all(map(lambda c: ord(c) >= 0x80 and ord(c) <= 0xbf, data[1:num])):
return num
else:
return -1
if ord(data[0]) < 128:
# ASCII char
return 1
elif ord(data[0]) & 0xe0 == …Run Code Online (Sandbox Code Playgroud)