python数据预处理 字符串_Python数据预处理(二)- 清洗文本数据

本文介绍了如何使用Python的正则表达式库re进行HTML数据的清洗,包括去除DOCTYPE、CDATA、HTML标签、注释、换行符、转义字符、空格以及超链接等,以实现文本的预处理。
摘要由CSDN通过智能技术生成

"""

Description:正则清洗HTML数据

Author:

Prompt: code in python3 env

"""

"""

re.I   使匹配对大小写不敏感

re.L   做本地化识别(locale-aware)匹配

re.M   多行匹配,影响^(开头)和$(结尾)

re.S   匹配包含换行在内的所有字符

re.U   根据Unicode字符集解析字符,这个标志影响 \w, \W, \b, \B

re.X   该标志通过给予你更灵活的格式以便你将正则表达式写得更加

"""

import re

# 处理HTML标签文本

# @param htmlstr html字符串

def filter_tags(htmlstr):

# 过滤doc_type

htmlstr = ' '.join(htmlstr.split())

re_doctype = re.compile(r'.*?>', re.S)

res = re_doctype.sub('', htmlstr)

# 过滤CDATA

re_cdata = re.compile( r'//] //\] >', re.I)

res = re_cdata.sub('', res)

# Script

re_script = re.compile(']*>[^', re.I)

res = re_script.sub('', res)

# 注释

re_script = re.compile('', 0)

res = re_script.sub('', res)

# 换行符

re_br = re.compile('
')

res = re_br.sub('\n', res)

# HTML 标签

re_lable = re.compile('?\w[^>]*>')

res = re_lable.sub('', res)

# 转义字符

re_esc = re.compile('&.*?;')

res = re_esc.sub('', res)

# 空格处理

re_blank = re.compile('\s+') # \s包含 \t \n \r \f \v

res = re_blank.sub(' ', res)

# 超链接处理

re_http = re.compile(r'(http://.+.html)')

res = re_http.sub(' ', res)

d = lambda pattern, flags=0: re.compile(pattern, flags)

for re_type in re_mate:

re_type = d(*re_type)

res = re_type.sub(' ', res)

return res

def read_file(read_path):

str_doc = ''

with open(read_path, 'r', encoding='utf-8') as f:

str_doc = f.read()

return str_doc

if __name__ == '__main__':

str_doc = read_file(r'../data/html/re.html')

res = filter_tags(str_doc)

# print(res)

with open(r'../data/html/test.html', 'w', encoding='utf-8') as f:

f.write(res)

print('No Exception') # 我是通过另一个编辑器进行打开预览的

这是我的笔记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值