如何在 Python 字符串中去掉逗号

我怎样才能从一个 Python 字符串(如 Foo, bar)中去除逗号呢? 我试过 'Foo, bar'.strip(','),但它不起作用。

196055 次浏览

You want to replace it, not strip it:

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

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

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

This will strip all commas from the text and left justify it.

for row in inputfile:
place = row['your_row_number_here'].strip(', ')

‎‎‎‎‎ ‎‎‎‎‎‎