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的一些主流方法和代码。

相关推荐
lskisme1 小时前
springboot maven导入本地jar包
开发语言·python·pycharm
开心-开心急了1 小时前
pyside6实现win10自动切换主题
开发语言·python·pyqt·pyside
mortimer2 小时前
一键实现人声伴奏分离:基于 `uv`, `FFmpeg` 和 `audio-separator` 的高效解决方案
python·ffmpeg·音视频开发
wudl55662 小时前
Pandas-之数据可视化
信息可视化·数据分析·pandas
Sunhen_Qiletian2 小时前
Python 类继承详解:深度学习神经网络架构的构建艺术
python·深度学习·神经网络
程序员大雄学编程3 小时前
用Python来学微积分34-定积分的基本性质及其应用
开发语言·python·数学·微积分
Q_Q5110082853 小时前
python+django/flask的莱元元电商数据分析系统_电商销量预测
spring boot·python·django·flask·node.js·php
林一百二十八3 小时前
Python实现手写数字识别
开发语言·python
Q26433650233 小时前
【有源码】基于Hadoop+Spark的起点小说网大数据可视化分析系统-基于Python大数据生态的网络文学数据挖掘与可视化系统
大数据·hadoop·python·信息可视化·数据分析·spark·毕业设计
中杯可乐多加冰4 小时前
基于网易CodeWave智能开发平台构建宝可梦图鉴
深度学习·低代码·ai·数据分析·数据采集·无代码·网易codewave征文