1

I have a script that reads an XML file and writes it into the Database.
When I run it through the browser (call it via a view) it works fine, but
when I created a Command for it (./manage.py importxmlfile) I get the following message:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 6: ordinal not in range(128)

I'm not sure why it would only happen when calling the import via command line.. any ideas?

Update
I'm trying to convert an lxml.etree._ElementUnicodeResult object to string and save it in the DB (utf8 collation) using str(result).
This produces the error mentioned above only on Command Line.

7
  • Can you post your source code? Commented Jun 9, 2013 at 18:21
  • it's a pretty massive project, not really an option. Commented Jun 9, 2013 at 18:24
  • odd, it works when I debug it on Eclipse, but with Konsole it doesn't Commented Jun 9, 2013 at 18:29
  • 1
    Try to add # -*- coding: utf-8 -*- on bottom of your command file. And for your objects's unicode methods try to return unicoded strings: return u"%s" %(self.param) Commented Jun 9, 2013 at 18:31
  • doesn't seem to have an effect Commented Jun 9, 2013 at 18:35

1 Answer 1

1

Ah, don't use str(result).

instead, do:

result.encode('utf-8')

When you call str(result), python will use the default system encoding (usually ascii) to try and encode the bytes in result. This will break if the ordinal not in range(128). Rather than using the ascii codec, just .encode() and tell python which codec to use.

Check out the Python Unicode HowTo for more information. You might also want to check out this related question or this excellent presentation on the subject.

Sign up to request clarification or add additional context in comments.

1 Comment

I had to make sure to use it when using regex (on an external function), and on some other pre-save functions, but worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.