如何去除Python字符串中的逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16233593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to strip comma in Python string
提问by msampaio
How can I strip the comma from a Python string such as Foo, bar? I tried 'Foo, bar'.strip(','), but it didn't work.
如何从 Python 字符串中去除逗号,例如Foo, bar?我试过了'Foo, bar'.strip(','),但没有用。
回答by pradyunsg
Use replacemethod of strings not strip:
使用replace字符串的方法 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
回答by maow
unicode('foo,bar').translate(dict([[ord(char), u''] for char in u',']))
unicode('foo,bar').translate(dict([[ord(char), u''] for char in u',']))
回答by Shal
This will strip all commas from the text and left justify it.
这将从文本中去除所有逗号并左对齐。
for row in inputfile:
place = row['your_row_number_here].strip(', ')

