How can I strip the comma from a Python string such as Foo, bar
? I tried 'Foo, bar'.strip(',')
, but it didn't work.
问问题
182085 次
5 回答
16
Use replace
method of strings not strip
:
s = s.replace(',','')
An example:
>>> 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
于 2013-04-26T09:58:24.990 回答
6
unicode('foo,bar').translate(dict([[ord(char), u''] for char in u',']))
于 2013-04-26T10:49:53.070 回答
1
This will strip all commas from the text and left justify it.
for row in inputfile:
place = row['your_row_number_here'].strip(', ')
</p>
于 2019-01-20T18:21:15.243 回答
0
You can use rstrip():
s = s.rstrip(",")
于 2021-08-06T06:39:32.827 回答