How to strip comma in Python string

妖精的绣舞 提交于 2019-11-29 02:01:33

问题


How can I strip the comma from a Python string such as Foo, bar? I tried 'Foo, bar'.strip(','), but it didn't work.


回答1:


You want to replace it, not strip it:

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



回答2:


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



回答3:


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




回答4:


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

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


来源:https://stackoverflow.com/questions/16233593/how-to-strip-comma-in-python-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!