views:

469

answers:

4

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 itself or using a Find Replace in my Python / Django code.

Any suggestions on how I can do this?

A: 

You can use that the ASCII characters are the first 128 ones, so get the number of each character with ord and strip it if it's out of range

# -*- coding: utf-8 -*-

def strip_non_ascii(string):
    ''' Returns the string without non ASCII characters'''
    stripped = (c for c in string if 0 < ord(c) < 127)
    return ''.join(stripped)


test = u'éáé123456tgreáé@€'
print test
print strip_non_ascii(test)

Result

éáé123456tgreáé@€
123456tgre@

Please note that @ is included because, well, after all it's an ASCII character. If you want to strip a particular subset (like just numbers and uppercase and lowercase letters), you can limit the range looking at a ASCII table

EDITED: After reading your question again, maybe you need to escape your HTML code, so all those characters appears correctly once rendered. You can use the escape filter on your templates.

Khelben
Zack
+1  A: 

I found this a while ago, so this isn't in any way my work. I can't find the source, but here's the snippet from my code.

def unicode_escape(unistr):
    """
    Tidys up unicode entities into HTML friendly entities

    Takes a unicode string as an argument

    Returns a unicode string
    """
    import htmlentitydefs
    escaped = ""

    for char in unistr:
        if ord(char) in htmlentitydefs.codepoint2name:
            name = htmlentitydefs.codepoint2name.get(ord(char))
            entity = htmlentitydefs.name2codepoint.get(name)
            escaped +="&#" + str(entity)

        else:
            escaped += char

    return escaped

Use it like this

>>> from zack.utilities import unicode_escape
>>> unicode_escape(u'such as ® I want')
u'such as &#174 I want'
Zack
A: 

You shouldn't have anything to do, as Django will automatically escape characters :

see : http://docs.djangoproject.com/en/dev/topics/templates/#id2

sebpiq
Zack
That's right, but it's the only ones he needs to escape !
sebpiq
A: 

To get rid of the special xml, html characters '<', '>', '&' you can use cgi.escape:

import cgi
test = "1 < 4 & 4 > 1"
cgi.escape(test)

Will return:

'1 &lt; 4 &amp; 4 &gt; 1'

This is probably the bare minimum you need to avoid problem. For more you have to know the encoding of your string. If it fit the encoding of your html document you don't have to do something more. If not you have to convert to the correct encoding.

test = test.decode("cp1252").encode("utf8")

Supposing that your string was cp1252 and that your html document is utf8

Vivian De Smedt