python判断字符串中是否只有ASCII编码字符,pythonascii,下面函数可以检查一个字符
python判断字符串中是否只有ASCII编码字符,pythonascii,下面函数可以检查一个字符
下面函数可以检查一个字符串中是否只包含ASCII字符:
TEXT_CHARS =("0000000111101100" # 0x0X "0000000000010000" # 0x1X This table give for each characters "1111111111111111" # 0x2X the kind of ASCII text file it can "1111111111111111" # 0x3X belong to. "1111111111111111" # 0x4X "1111111111111111" # 0x5X "1111111111111111" # 0x6X 0 never appears in text "1111111111111110" # 0x7X 1 appears in plain ASCII text "3333313333333333" # 0x8X 2 appears in ISO-8859 text "3333333333333333" # 0x9X 3 appears in non-ISO extended- "2222222222222222" # 0xaX ASCII (Mac, IBM PC) "2222222222222222" # 0xbX "2222222222222222" # 0xcX "2222222222222222" # 0xdX This table is copyrighted, "2222222222222222" # 0xeX see the discussion part. "2222222222222222")# 0xfX PLAIN_ASCII = ''.join([chr(i) for i in range(256) if TEXT_CHARS[i]=='1'])def ascii_encoding(s): """ return 0 if the text s is not an ascii text, 1 if the text is a plain ASCII text, 2 if the text is ISO-8859, 3 if the file is an non ISO extended text file""" s = s.translate(TEXT_CHARS, PLAIN_ASCII) for i in "032": if i in s: return int(i) return 1## some samples#print ascii_encoding("Hello wolrd")print ascii_encoding("Sébastien Keim")print ascii_encoding("AZZ\x12BB")
相关内容
- python使用Queue实现优先级队列,pythonqueue,使用Queue.Queu
- python多线程之使用ReadWriteLock,,ReadWriteLoc
- python 调用windows api查看系统的电量,pythonapi,通过调用
- 显示django中的所有url设置,显示djangourl设置,下面的脚本
- python给数字加千分位,每隔三位加逗号,python千分,如题
- python list常用操作封装,pythonlist封装,如下是一系列li
- python 计算时间差,时间加减运算代码,python时间差,Q
- python复制某文件夹下指定扩展名的文件,并且保留原目
- python使用正则表达式实现的javascript文件压缩代码,py
- python使用baidu服务器时间设置操作系统时间,pythonbaid
评论关闭