uni-code error in python : 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

35 views
Skip to first unread message

Hossein Rashnoo

unread,
Dec 29, 2014, 2:33:49 AM12/29/14
to django...@googlegroups.com
Hi
I use django and in my view i need to send a request as XML with some uni-code character that received from html page with post method. I tried these (Note that i save that input in fname variable)  : 

    xml = r"""my XML code with uni-code {0} """.format(fname)

And

    fname = u"%s".encode('utf8') % (fname)
    xml = r"""my XML code with uni-code {0} """.format(fname)

And

    fname = fname.encode('ascii', 'ignore').decode('ascii')
    xml = r"""my XML code with uni-code {0} """.format(fname)

And every time i got this error:

    'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

Please help me.

Hossein Rashnoo

unread,
Dec 29, 2014, 2:52:24 AM12/29/14
to django...@googlegroups.com

I Solved that with this code:

fname = fname.encode('ascii', 'xmlcharrefreplace')
xml = r"""my XML code with unicode {0} """.format(fname)
Reply all
Reply to author
Forward
0 new messages