一个中文utf8编码后是占3个字符,所以求长度的函数可以这样写
def str_len(str): try: row_l=len(str) utf8_l=len(str.encode('utf-8')) return (utf8_l-row_l)/2+row_l except: return None return None
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did86627
一个中文utf8编码后是占3个字符,所以求长度的函数可以这样写
def str_len(str): try: row_l=len(str) utf8_l=len(str.encode('utf-8')) return (utf8_l-row_l)/2+row_l except: return None return None
上一篇: Python异步调用命令行工具