วันพฤหัสบดีที่ 7 พฤษภาคม พ.ศ. 2552

python check message is thai language

code แบบง่าย ๆ เอาไปใช้ก่อน
#function long

def isthaistr(s):
    for i in s:
        if 255 < ord(i):
            return True
    return False
#function short

def isthaistr(s):
    return any( ord( c ) > 255 for c in s ) 
ในกรณีที่ข้อความเราได้มานั้น เป็น TIS-620 เราอาจต้องแปลงเป็น UTF-8 โดย encode มันด้วยสำสั่งแบบนี้
s = 'ก'
print ord(s) # 161
s = s.decode('its-620')
pirnt ord(s) # 3585

s = 'a'
print ord(s) # 97
s = s.decode('tis-620')
print ord(s) # 97
ประมาณนี้...

ไม่มีความคิดเห็น: