python 去除字符串中各种非字母,非数字符号,把字符串用做文件名

版权声明:欢迎转载。转载请注明地址:https://blog.csdn.net/weixin_32820767 https://blog.csdn.net/weixin_32820767/article/details/82314867

有一行字符串:

void future::work::related<exist::fluid<std::int, std::float>>(std::float=1)

改成:

void_future__work__related_exist__fluid_std__int__std__float___std__float_1_

我们想去掉其中的非字母,非数字的符号,并用下划线代替,并把它作为文件名。

    def validateTitle(self, title):
        """ 将 title 名字 规则化
        :param title: title name 字符串
        :return: 文件命名支持的字符串
        """
        rstr = r"[\=\(\)\,\/\\\:\*\?\"\<\>\|\' ']"  # '= ( ) , / \ : * ? " < > |  '   还有空格
        new_title = re.sub(rstr, "_", title)  # 替换为下划线
        return new_title

上述方法非常有效。

猜你喜欢

转载自blog.csdn.net/weixin_32820767/article/details/82314867
今日推荐