※ 引述《huggie (huggie)》之銘言:
: 我有一些 utf-8 資料,我想要知道某個字元是否是英文字母 (而非中文字),
: 要如何做最好?我想過要轉成 ascii code 再比大小,結果不知道怎麼轉。
: 現在我的作法就是去看現在這個字元是否是 string.ascii_letters 的子字串。
: 這樣會不會太笨了點...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.59.232.102
以下,供參考
>>> outfile = open("tmp.utf8", "wb")
>>> outfile.write("English.中文。".decode("big5").encode("utf8"))
>>> outfile.close()
>>> infile = open("tmp.utf8", "r")
>>> chars = infile.read().decode("utf8")
>>> infile.close()
>>> print chars.encode("big5")
English.中文。
>>> chars[0].isalpha()
True
>>> chars[-1].isalpha()
False
>>> chars[-2].isalpha()
True
>>> u"A" <= chars[0] <= u"Z"
True
>>> u"a" <= chars[0] <= u"z"
False
>>> u"a" <= chars[1] <= u"z"
True
>>>