2013-04-26 2166 views

回答

75

你想replace它,而不是strip它:

s = s.replace(',', '') 
+3

我敢打赌,有人会发布此正则表达式版本... – jamylak 2013-04-26 09:58:04

+10

@jamylak's = re.sub(',','',s)';) – Kiro 2013-04-26 10:19:42

9

使用replace字符串的方法不strip

s = s.replace(',','') 

一个例子:

>>> s = 'Foo, bar' 
>>> s.replace(',',' ') 
'Foo bar' 
>>> s.replace(',','') 
'Foo bar' 
>>> s.strip(',') # clears the ','s at the start and end of the string which there are none 
'Foo, bar' 
>>> s.strip(',') == s 
True 
+0

男人!很明显!如此显而易见!但很明显,我正在使用脱衣舞,并试图了解为什么它不能作为批量替换... – 2017-06-21 11:06:52

3

unicode('foo,bar').translate(dict([[ord(char), u''] for char in u',']))

+0

+1哈哈哈我希望这是一个笑话 – jamylak 2013-04-26 14:12:01

相关问题