Remove non-ASCII characters from a string using python / django

前端 未结 6 488
情歌与酒
情歌与酒 2020-12-05 19:11

I have a string of HTML stored in a database. Unfortunately it contains characters such as ® I want to replace these characters by their HTML equivalent, either in the DB it

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 19:47

    There's a much simpler answer to this at https://stackoverflow.com/a/18430817/5100481

    To remove non-ASCII characters from a string, s, use:

    s = s.encode('ascii',errors='ignore')

    Then convert it from bytes back to a string using:

    s = s.decode()

    This all using Python 3.6

提交回复
热议问题