本文翻译自:Stripping everything but alphanumeric chars from a string in Python

What is the best way to strip all non alphanumeric characters from a string, using Python? 使用Python从字符串中剥离所有非字母数字字符的最佳方法是什么?

The solutions presented in the PHP variant of this question will probably work with some minor adjustments, but don't seem very 'pythonic' to me. 这个问题的PHP变体中提供的解决方案可能会进行一些小的调整,但对我来说似乎并不是很“ pythonic”。

For the record, I don't just want to strip periods and commas (and other punctuation), but also quotes, brackets, etc. 作为记录,我不仅要删除句点和逗号(以及其他标点符号),还希望删除引号,方括号等。


#1楼

参考:https://stackoom.com/question/5M8y/从Python字符串中剥离字母数字字符以外的所有内容


#2楼

Regular expressions to the rescue: 救援的正则表达式:

import re
re.sub(r'\W+', '', your_string)

By Python definition '\\W == [^a-zA-Z0-9_] , which excludes all numbers , letters and _ 通过Python定义'\\W == [^a-zA-Z0-9_] ,其中排除了所有numbersletters_


#3楼

How about: 怎么样:

def ExtractAlphanumeric(InputString):from string import ascii_letters, digitsreturn "".join([ch for ch in InputString if ch in (ascii_letters + digits)])

This works by using list comprehension to produce a list of the characters in InputString if they are present in the combined ascii_letters and digits strings. 如果组合的ascii_lettersdigits字符串中存在字符,则可以使用列表ascii_letters来生成InputString中的字符列表。 It then joins the list together into a string. 然后,它将列表连接到一个字符串中。


#4楼

>>> import re
>>> string = "Kl13@£$%[};'\""
>>> pattern = re.compile('\W')
>>> string = re.sub(pattern, '', string)
>>> print string
Kl13

#5楼

您可以尝试:

print ''.join(ch for ch in some_string if ch.isalnum())

#6楼

I just timed some functions out of curiosity. 我只是出于好奇而对某些功能进行了计时。 In these tests I'm removing non-alphanumeric characters from the string string.printable (part of the built-in string module). 在这些测试中,我从字符串string.printable (内置string模块的一部分)中删除了非字母数字字符。 The use of compiled '[\\W_]+' and pattern.sub('', str) was found to be fastest. 发现编译后的'[\\W_]+'pattern.sub('', str)使用最快。

$ python -m timeit -s \"import string" \"''.join(ch for ch in string.printable if ch.isalnum())"
10000 loops, best of 3: 57.6 usec per loop$ python -m timeit -s \"import string" \"filter(str.isalnum, string.printable)"
10000 loops, best of 3: 37.9 usec per loop$ python -m timeit -s \"import re, string" \"re.sub('[\W_]', '', string.printable)"
10000 loops, best of 3: 27.5 usec per loop$ python -m timeit -s \"import re, string" \"re.sub('[\W_]+', '', string.printable)"
100000 loops, best of 3: 15 usec per loop$ python -m timeit -s \"import re, string; pattern = re.compile('[\W_]+')" \"pattern.sub('', string.printable)"
100000 loops, best of 3: 11.2 usec per loop

从Python字符串中剥离字母数字字符以外的所有内容相关推荐

  1. Python中的函数及Python字符串中提取字母、数字

    一.函数:就是对功能或动作的封装,一段有规律.重复使用的代码. 函数的定义: def 函数名(形参): 函数体 返回值(return) 函数的调用: ret=函数名(实参) 函数的返回值 return ...

  2. python电话号码对应的字符组合_Python3 在字符串中提取字母+数字组合微信账号、电话等 - pytorch中文网...

    今天处理数据要提取字符串中的微信,字符串中包含中文英文Emoji,标点符号等. python 提取字符串中的电话 提取电话相对简单,多个电话也可以提取 import re desstr = " ...

  3. python字符串替换源码_Python实现字符串中某个字母的替代功能

    Python实现字符串中某个字母的替代功能 今晚想实现这样一个功能:将输入字符串中的字母 "i" 变成字母 "p".当时想的很简单,直接用for循环遍历,然后替 ...

  4. 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数

    统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数.(不考虑其他字符) 1.需求: 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数.(不考虑其他字符) 举例: " ...

  5. Python实现——依次计算一系列给定字符串的字母值,字母值为字符串中每个字母对应的编号值(A对应1,B对应2,以此类推,不区分大小写字母,非字母字符对应的值为0)的总和。

    题目内容: 依次计算一系列给定字符串的字母值,字母值为字符串中每个字母对应的编号值(A对应1,B对应2,以此类推,不区分大小写字母,非字母字符对应的值为0)的总和.例如,Colin 的字母值为 3 + ...

  6. Python:如何从字符串中提取字母或数字?

    从字符串中提取字母 s = 'cn中国520' print(''.join([i for i in s if i.encode('UTF-8').isalpha()]))# 输出:cn 注意:中文的汉 ...

  7. python中表示换行的符号_对Python字符串中的换行符和制表符介绍

    下面为大家分享一篇对Python字符串中的换行符和制表符介绍,具有很好的参考价值,希望对大家有所帮助.一起过来看看吧 有关换行的问题 首先提一个问题,如下. python程序代码如下: print(& ...

  8. C语言编程>第二十六周 ⑥ 请补充fun函数,该函数的功能是:按 “0”到 “9”统计一个字符串中的奇数数字字符各自出现的次数,结果保存在数组num中。注意:不能使用字符串库函数。

    例题:请补充fun函数,该函数的功能是:按 "0"到 "9"统计一个字符串中的奇数数字字符各自出现的次数,结果保存在数组num中.注意:不能使用字符串库函数. ...

  9. 35、统计字符串中大写字母、小写字母、数字和空格出现的次数

    题目 统计字符串中大写字母.小写字母.数字和空格出现的次数 解法一 遍历字符串,使用字符串内建函数判断 代码如下: #!/usr/bin/python # -*- coding:UTF-8 -*-cl ...

最新文章

  1. python 把列表或者元组转成集合
  2. Detach Volume 操作 - 每天5分钟玩转 OpenStack(55)
  3. 小程序之获取用户信息取消授权处理
  4. 【2021年度训练联盟热身训练赛第二场】Tip to be Palindrome(python)
  5. delphi之找色和色块
  6. 软件c#语言调用摄像头,C#调用摄像头的几种方式
  7. mysql主从复制周期_Mysql主从复制的实现
  8. 【CSS3】将截断的文字可选的显示出来
  9. 广州程序员辞职创业卖菠萝油,放弃30万年薪
  10. Python内置的字符串处理函数整理
  11. js去除字符串数字前面的0_JS正则里面“?”的用处
  12. 鸿蒙硬件HI3861-MQTT-连接华为云
  13. VOIP术语及相关知识
  14. SIP中第三方呼叫控制(3PCC)建立流程
  15. 毕设一:python 爬取苏宁的商品评论
  16. 2021研究生数学建模B题,空气质量检测
  17. 线性系统理论和设计 (仝茂达)习题答案
  18. 计算机组装与维护致谢,浅谈计算机组装与维护的教学改革-毕业论文致谢
  19. Word文档没保存电脑死机了,重启打开文档一片空白怎么办?
  20. 【面经】2018金山WPS前端笔试题 面试题

热门文章

  1. MySQL 5.7.18忘记密码和密码过期解决
  2. 一种在旧代码上增加新需求的重构模式
  3. android:inputType 参数详解
  4. jfreechart 多参数传递
  5. MacPE+WinPE-黑苹果之路
  6. IE与FireFox的不同点(不断更新中..)
  7. numpy学习之创建数组
  8. 《机器学习实战》学习笔记第七章 —— AdaBoost元算法
  9. Birt报表安装及制作
  10. iOS中UINavigationController控制器使用详解