Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么re.sub返回None?_Python_Regex - Fatal编程技术网

Python 为什么re.sub返回None?

Python 为什么re.sub返回None?,python,regex,Python,Regex,我在github上的一些代码中看到了这个函数,我认为它的目的是清除输入文本中的这些符号,但我不知道为什么输入到这个函数中的每个字符串都会变成无。感谢您的帮助 def clean(self, text): text = re.sub('[^\w\s\d\'\-\"]','', text) text = text.lower() 缺少显式return语句的Python函数隐式返回None。clean函数没有返回语句。您可以尝试添加返回文本请修复python缩进。还有,产出是如何产生

我在github上的一些代码中看到了这个函数,我认为它的目的是清除输入文本中的这些符号,但我不知道为什么输入到这个函数中的每个字符串都会变成无。感谢您的帮助

def clean(self, text):
    text = re.sub('[^\w\s\d\'\-\"]','', text)
    text = text.lower()

缺少显式
return
语句的Python函数隐式返回
None
clean
函数没有返回语句。您可以尝试添加
返回文本

请修复python缩进。还有,产出是如何产生的?请包括,因为函数不返回任何内容。谢谢,没错。你知道re.sub到底在这里干什么吗?在re.sub的其他示例中,我没有看到使用[]。非常感谢您,这是正确的。你知道re.sub到底在这里干什么吗?我在re.sub的其他示例中没有看到使用[]。@parvaneh
sub
表示“替换”。它用
'
(无)替换文本中正则表达式模式的实例。在这种情况下,它将删除不在指定类中的所有字符。@parvaneh
[^bar]
匹配除
b
a
r
以外的任何字符。
[]
指定一个集合,
[^]
对其进行否定。一些
\
表单也表示字符集。请参阅
re
模块文档。