简体   繁体   中英

can not remove comma from unicode string in Python

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?

Try on the same line like,

>>>>import string
>>>>transtab = string.maketrans(",", " ")
>>>>unicodestring = r'Phoenix Electronics, LLC'
>>>>unicodestring.translate(transtab)
    'Phoenix Electronics  LLC'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM