通过使用python来清洗数据的技巧持续更新:【内向即失败--王奕君】

来源:互联网 发布:基站定位软件 编辑:程序博客网 时间:2024/06/06 00:33
# - * - coding:utf8 -import stringimport redef clean_html(text:'str'):    '''html解码'''    text = text.replace('&amp;', '&')    text = text.replace('&lt;', '<')    text = text.replace('&gt;', '>')    text = text.replace('&nbsp;', '')    text = text.replace('&#39;', "\'")    text = text.replace('&quot;', '\"')    text = text.replace('<br>;', '\n')    return textdef clean_space(text:'str'):    '''去掉\r\n以及空格'''    text = text.replace('\r', '')    text = text.replace('\n', '')    text=text.replace(' ','')    return textdef clean_symbol(text:'str'):    '''去掉所有符号'''    return re.sub("[\.\!\/_,$%^*(+\"\']+|[+—!,。?、~@#¥%…&*()]+", "", text)def clean_bothnumber(text:'str'):    '''去掉两边数字'''    return text.strip(string.digits)def clean_listspace(tabulation:'object()'):    '''去掉列表\r\n以及空格       最终返回列表    '''    if isinstance(tabulation,(list)):        transform_tabulation=str(tabulation)        text = transform_tabulation.replace('\r', '')        text = transform_tabulation.replace('\n', '')        text=transform_tabulation.replace(' ','')        return eval(text)    elif isinstance(tabulation,(str)):        text = tabulation.replace('\r', '')        text = tabulation.replace('\n', '')        text = tabulation.replace(' ', '')        return eval(text)def clean_bothsymbol(text:'str'):    '''去掉两边的所有符号'''    return text.strip(string.punctuation)def clean_custom(text:'str',container:'list'):    '''去掉自定义文本'''    for con in container:        text=text.replace(con,'')    return textdef clean_script(text:'str'):    '''去除html脚本'''    return re.compile(r'<script.*?</script>',re.I | re.S).sub('',text)def clean_rapecode(text:'str'):    '''去掉html注释'''    return re.compile(r'<!.*?>', re.I | re.S).sub('',text)def clean_htmlcode(text:'str'):    '''去掉html标签'''    return re.compile(r'<.*?/{0,1}>',re.I).sub('',text)def clean_englishsymbol(text:'str'):    '''去掉英文前后所有的英文符号'''
阅读全文
0 0