如何去除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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:04:17  来源:igfitidea点击:

How to strip comma in Python string

pythonstringstrip

提问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 eumiro

You want to replaceit, not stripit:

你想要replace它,而不是strip它:

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

回答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(', ')