如何使用 Python 从字符串中删除符号? [英] How to remove symbols from a string with Python?
本文介绍了如何使用 Python 从字符串中删除符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Python 和 RegEx 的初学者,我想知道如何制作一个接受符号并用空格替换它们的字符串.任何帮助都很棒.
例如:
枫糖浆多少钱?20.99 美元?太荒谬了!!!
进入:
枫糖浆多少钱 20 99 太可笑了
解决方案
一种方法,使用 正则表达式:
<预><代码>>>>s =枫糖浆多少钱?20.99美元?这太荒谬了!!!">>>re.sub(r'[^\w]', ' ', s)枫糖浆多少钱 20 99 太荒谬了"\w
将匹配字母数字字符和下划线[^\w]
将匹配任何非字母数字或下划线
I'm a beginner with both Python and RegEx, and I would like to know how to make a string that takes symbols and replaces them with spaces. Any help is great.
For example:
how much for the maple syrup? $20.99? That's ricidulous!!!
into:
how much for the maple syrup 20 99 That s ridiculous
解决方案
One way, using regular expressions:
>>> s = "how much for the maple syrup? $20.99? That's ridiculous!!!"
>>> re.sub(r'[^\w]', ' ', s)
'how much for the maple syrup 20 99 That s ridiculous '
\w
will match alphanumeric characters and underscores[^\w]
will match anything that's not alphanumeric or underscore
这篇关于如何使用 Python 从字符串中删除符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文