如何替换字符串中的多个字符? [英] how to replace multiple characters in a string?

查看:64
本文介绍了如何替换字符串中的多个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何替换字符串中的多个字符?

how to replace multiple characters in a string?

请帮忙修改脚本

我需要将name"行中的特殊字符替换为短语special char"

I need to in the line "name" special characters have been replaced by the phrase "special char"

newName = replace(name, ['\', '/', ':', '*', '?', '"', '<', '>', '|'], 'special char')

但我收到消息:

无效语法

推荐答案

您可以使用 re.sub():

import re
newName = re.sub('[\\\\/:*?"<>|]', 'special char', name)

这篇关于如何替换字符串中的多个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆