Python - Removing Punctuation

本文介绍了一种使用Python从文本数据中移除标点符号的方法。通过定义一个函数,利用unicode字符类别来创建一个标点符号字典,并使用translate方法进行替换。此方法适用于批量文本数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem: You've a feature of text data and want to remove punctuaiton

Soluation: Define a function that uses translate with a dictionary of punctuation characters:

# Load libraries
import unicodedata
import sys

# Create text
text_data = ['Hi!!!! I. Love. This. Song....',
 '10000% Agree!!!! #LoveIT',
 'Right?!?!']

# Create a dictionary of punctuation characters
punctuation = dict.fromkeys(i for i in range(sys.maxunicode)
 if unicodedata.category(chr(i)).startswith('P'))

# For each string, remove any punctuation characters
[string.translate(punctuation) for string in text_data]

result: ['Hi I Love This Song', '10000 Agree LoveIT', 'Right']

 

from "Chris_Albon]_Machine_Learning_with_Python_Cookbook"

x@pi:~/WiringPi/WiringPi $ sudo apt install -y python3-rpi.gpio python3-spidev python3-can # 通过 pip 安装最新版 (可选) sudo pip3 install RPi.GPIO spidev python-can Reading package lists... Done Building dependency tree... Done Reading state information... Done python3-spidev is already the newest version (20200602~200721-1+bookworm). The following additional packages will be installed: python3-msgpack python3-packaging rpi.gpio-common Suggested packages: python-can-doc The following packages will be REMOVED: python3-rpi-lgpio The following NEW packages will be installed: python3-can python3-msgpack python3-packaging python3-rpi.gpio rpi.gpio-common 0 upgraded, 5 newly installed, 1 to remove and 159 not upgraded. Need to get 301 kB of archives. After this operation, 1,467 kB of additional disk space will be used. Get:2 http://mirrors.zju.edu.cn/raspbian/raspbian bookworm/main armhf python3-packaging all 23.0-1 [32.5 kB] Get:1 http://raspbian.raspberrypi.com/raspbian bookworm/main armhf python3-msgpack armhf 1.0.3-2+b1 [62.0 kB] Get:3 http://mirrors.zju.edu.cn/raspbian/raspbian bookworm/main armhf python3-can all 4.1.0-1 [179 kB] Get:4 http://raspbian.raspberrypi.com/raspbian bookworm/main armhf rpi.gpio-common armhf 0.7.1~a4-1+b2 [6,808 B] Get:5 http://raspbian.raspberrypi.com/raspbian bookworm/main armhf python3-rpi.gpio armhf 0.7.1~a4-1+b2 [21.2 kB] Fetched 301 kB in 4s (85.8 kB/s) (Reading database ... 137316 files and directories currently installed.) Removing python3-rpi-lgpio (0.6-0~rpt1) ... Selecting previously unselected package python3-msgpack. (Reading database ... 137301 files and directories currently installed.) Preparing to unpack .../python3-msgpack_1.0.3-2+b1_armhf.deb ... Unpacking python3-msgpack (1.0.3-2+b1) ... Selecting previously unselected package python3-packaging. Preparing to unpack .../python3-packaging_23.0-1_all.deb ... Unpacking python3-packaging (23.0-1) ... Selecting previously unselected package python3-can. Preparing to unpack .../python3-can_4.1.0-1_all.deb ... Unpacking python3-can (4.1.0-1) ... Selecting previously unselected package rpi.gpio-common:armhf. Preparing to unpack .../rpi.gpio-common_0.7.1~a4-1+b2_armhf.deb ... Unpacking rpi.gpio-common:armhf (0.7.1~a4-1+b2) ... Selecting previously unselected package python3-rpi.gpio. Preparing to unpack .../python3-rpi.gpio_0.7.1~a4-1+b2_armhf.deb ... Unpacking python3-rpi.gpio (0.7.1~a4-1+b2) ... Setting up rpi.gpio-common:armhf (0.7.1~a4-1+b2) ... Setting up python3-rpi.gpio (0.7.1~a4-1+b2) ... Setting up python3-packaging (23.0-1) ... Setting up python3-msgpack (1.0.3-2+b1) ... Setting up python3-can (4.1.0-1) ... Processing triggers for man-db (2.11.2-2) ... error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try apt install python3-xyz, where xyz is the package you are trying to install. If you wish to install a non-Debian-packaged Python package, create a virtual environment using python3 -m venv path/to/venv. Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make sure you have python3-full installed. For more information visit http://rptl.io/venv note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. hint: See PEP 668 for the detailed specification. x@pi:~/WiringPi/WiringPi $
最新发布
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值