Python数据分析从入门到进阶:快速处理文本(含代码)

🍁1. 清洗文本

对一些非结构化的文本数据进行基本的清洗

  • strip
  • split
  • replace
python 复制代码
# 创建文本
text_data = ['   Interrobang. By Aishwarya Henriette   ',
             'Parking And goding. by karl fautier',
             '   Today is the night. by jarek prakash    ']
python 复制代码
# 去除文本两端的空格
stripwhitespace = [string.strip() for string in text_data]
python 复制代码
stripwhitespace
css 复制代码
['Interrobang. By Aishwarya Henriette', 'Parking And goding. by karl fautier', 'Today is the night. by jarek prakash']
python 复制代码
# 删除句号
remove_periods = [string.replace('.','') for string in text_data]
python 复制代码
remove_periods
css 复制代码
['   Interrobang By Aishwarya Henriette   ', 'Parking And goding by karl fautier', '   Today is the night by jarek prakash    ']
python 复制代码
# 创建函数
def capitalizer(string):
    return string.upper()
python 复制代码
[capitalizer(string) for string in remove_periods]
css 复制代码
['   INTERROBANG BY AISHWARYA HENRIETTE   ', 'PARKING AND GODING BY KARL FAUTIER', '   TODAY IS THE NIGHT BY JAREK PRAKASH    ']
python 复制代码
# 使用正则表达式
import re
python 复制代码
def replace_letters_with_x(string):
    return re.sub(r'[a-zA-Z]','x',string)
python 复制代码
[replace_letters_with_x(string) for string in remove_periods]
css 复制代码
['   xxxxxxxxxxx xx xxxxxxxxx xxxxxxxxx   ', 'xxxxxxx xxx xxxxxx xx xxxx xxxxxxx', '   xxxxx xx xxx xxxxx xx xxxxx xxxxxxx    ']

🍂2. 解析并清洗HTML

python 复制代码
#使用beautiful soup 对html进行解析
python 复制代码
from bs4 import BeautifulSoup
python 复制代码
# 创建html代码
html = """
        <div class='full_name'><span style='font-weight:bold'>
        Masege Azra"
    
    """
python 复制代码
# 创建soup对象
soup = BeautifulSoup(html, 'lxml')
python 复制代码
soup.find('div')
xml 复制代码
<div class="full_name"><span style="font-weight:bold">
        Masege Azra"
    
    </span></div>

🍃3. 移除标点

python 复制代码
import unicodedata
import sys
python 复制代码
text_data = ['Hi!!!! I. love. This. Song....',
             '10000% Agree!!!! #LoveIT',
             'Right??!!']
python 复制代码
# 创建一个标点符号字典
punctuation = dict.fromkeys(i for i in range(sys.maxunicode) if unicodedata.category(chr(i)).startswith('P'))
python 复制代码
[string.translate(punctuation) for string in text_data]
css 复制代码
['Hi I love This Song', '10000 Agree LoveIT', 'Right']

🌍4. 文本分词

这里介绍一下jieba库

python 复制代码
python 复制代码
import jieba
python 复制代码
# 创建文本
string = 'The science of study is the technology of tomorrow'
python 复制代码
seg = jieba.lcut(string)
print(seg)
css 复制代码
['The', ' ', 'science', ' ', 'of', ' ', 'study', ' ', 'is', ' ', 'the', ' ', 'technology', ' ', 'of', ' ', 'tomorrow']

当然,本文只是介绍了在数据清洗中的一些最基本的文本处理方法,后续还会介绍目前NLP的一些主流方法和代码。

相关推荐
烛阴4 小时前
简单入门Python装饰器
前端·python
好开心啊没烦恼5 小时前
Python 数据分析:numpy,说人话,说说数组维度。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy
面朝大海,春不暖,花不开5 小时前
使用 Python 实现 ETL 流程:从文本文件提取到数据处理的全面指南
python·etl·原型模式
2301_805054566 小时前
Python训练营打卡Day59(2025.7.3)
开发语言·python
万千思绪6 小时前
【PyCharm 2025.1.2配置debug】
ide·python·pycharm
涤生大数据8 小时前
Apache Spark 4.0:将大数据分析提升到新的水平
数据分析·spark·apache·数据开发
微风粼粼8 小时前
程序员在线接单
java·jvm·后端·python·eclipse·tomcat·dubbo
云天徽上8 小时前
【PaddleOCR】OCR表格识别数据集介绍,包含PubTabNet、好未来表格识别、WTW中文场景表格等数据,持续更新中......
python·ocr·文字识别·表格识别·paddleocr·pp-ocrv5
可观测性用观测云8 小时前
Pipeline 引用外部数据源最佳实践
数据分析
你怎么知道我是队长8 小时前
python-input内置函数
开发语言·python