Python 字符串只保留汉字

借鉴了 https://blog.csdn.net/u012155582/article/details/78587394

这里使用Python3

def is_chinese(uchar):
    if uchar >= '\u4e00' and uchar <= '\u9fa5':
        return True
    else:
        return False

def reserve_chinese(content):
    content_str = ''
    for i in content:
        if is_chinese(i):
            content_str += i
    return content_str

还挺简单明了的,用在中文文本预处理中比较合适。

你可能感兴趣的:(python杂学)