I need to convert an arbitrary string to a string that is a valid variable name in python.
Here's a very basic example:
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
That is a very naive approach. I need to check if the string contains invalid
variable name characters and replace them with ''
What would be a pythonic way to do this ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…