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

前端 未结 6 474
情歌与酒
情歌与酒 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:49

    This code snippet may help you.

    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    
    def removeNonAscii(string):
        nonascii = bytearray(range(0x80, 0x100))
        return string.translate(None, nonascii)
    
    nonascii_removed_string = removeNonAscii(string_to_remove_nonascii)
    

    The encoding definition is very important here which is done in the second line.

提交回复
热议问题