python包含中文字符串长度

2016-01-23 0 977
python包含中文字符串长度

一个中文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 python包含中文字符串长度 http://www.ox520.com/16054.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务