主要是去除掉换行符、空格、制表符以及无效的字符:

import collections
import re
import unicodedata
import six

def clean_br(text):
    br_pattern = ('<br\s*?/?>')
    text = re.sub(br_pattern, '', text)
    return text

def is_control(char):
    """Checks whether `chars` is a control character."""
    # These are technically control characters but we count them as whitespace
    # characters.
    if char == "\t" or char == "\n" or char == "\r":
        return False
    cat = unicodedata.category(char)
    if cat in ("Cc", "Cf"):
        return True
    return False

def is_whitespace(char):
    """Checks whether `chars` is a whitespace character."""
    # \t, \n, and \r are technically contorl characters but we treat them
    # as whitespace since they are generally considered as such.
    if char == " " or char == "\t" or char == "\n" or char == "\r":
        return True
    cat = unicodedata.category(char)
    if cat == "Zs":
        return True
    return False

def clean_text(text):
    """Performs invalid character removal and whitespace cleanup on text."""
    output = []
    for char in text:
        cp = ord(char)
        if cp == 0 or cp == 0xfffd or is_control(char):
            continue
        if is_whitespace(char):
            output.append("")
        else:
            output.append(char)
    return clean_br("".join(output))


if __name__ == '__main__':
   text = '我爱<br> × 北京<br/>	天安门\n。哈哈'
   print(clean_text(text))

相关文章:

  • 2021-12-25
  • 2021-11-08
  • 2021-12-14
猜你喜欢
  • 2021-09-10
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2021-07-23
  • 2022-12-23
  • 2022-01-21
相关资源
相似解决方案