如何在Python字符串中剥离逗号


78

如何从诸如Python之类的字符串中去除逗号Foo, bar?我尝试过'Foo, bar'.strip(','),但是没有用。

Answers:



15

使用replace字符串的方法不是strip

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

一个例子:

>>> 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

人!太明显了!太明显了!但是如此明显,以至于我在使用脱衣舞并试图理解为什么它不能作为批量替代品...
Jayme Tosi Neto

6

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


+2表示除了整体可笑之外还成为
单线客

1

这将从文本中去除所有逗号,并使其左对齐。

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

‎ ‎‎‎‎‎ ‎‎‎‎‎‎

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.