我需要将任意字符串转换为python中有效变量名称的字符串.
这是一个非常基本的例子:
s1 = 'name/with/slashes'
s2 = 'name '
def clean(s):
s = s.replace('/','')
s = s.strip()
return s
print clean(s1)+'_'#the _ is there so I can see the end of the string
Run Code Online (Sandbox Code Playgroud)
这是一种非常天真的方法.我需要检查字符串是否包含无效的变量名字符并将其替换为''
什么是pythonic方式来做到这一点?