0

I would like to remove a comma from a string in Python. This is the code that I am using:

        next_column = unicode(my_value)
        next_column.replace(",", " ")
        next_column.translate(dict([[ord(char), u''] for char in u',']))
        next_column.translate(dict([[ord(char), None] for char in u',']))
        if my_key == "practice_name":
            nc = str(next_column)
            nc.replace(",", " ")
            nc.replace(',', " ")
            pprint(nc)

The pprint shows:

'Phoenix Electronics, LLC'

The comma is still there.

"my_value" comes from a Postgres database, an old legacy app where the company failed to check the encoding of what was going into the database.

I don't know if this is a code issue or an encoding issue. Does anyone have any suggestions?

cerhovice
  • 676
  • 1
  • 10
  • 24
  • 2
    Strings are immutable in Python. `nc.replace(",", " ")` is returning a new string, with the character removed, but not modifying `nc`. A possible duplicate would be the (poorly titled) question [python string replace](http://stackoverflow.com/questions/9189172/python-string-replace) – eldarerathis Dec 06 '14 at 07:05
  • Thanks much! After so many years with Ruby, I forget Python has some immutable data types. – cerhovice Dec 06 '14 at 07:44

1 Answers1

0

Try on the same line like,

>>>>import string
>>>>transtab = string.maketrans(",", " ")
>>>>unicodestring = r'Phoenix Electronics, LLC'
>>>>unicodestring.translate(transtab)
    'Phoenix Electronics  LLC'
kmario23
  • 57,311
  • 13
  • 161
  • 150