Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8165955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:41:37+00:00 2026-06-06T19:41:37+00:00

I have read the HOWTO on Unicode from the official docs and a full,

  • 0

I have read the HOWTO on Unicode from the official docs and a full, very detailed article as well. Still I don’t get it why it throws me this error.

Here is what I attempt: I open an XML file that contains chars out of ASCII range (but inside allowed XML range). I do that with cfg = codecs.open(filename, encoding='utf-8, mode='r') which runs fine. Looking at the string with repr() also shows me a unicode string.

Now I go ahead and read that with parseString(cfg.read().encode('utf-8'). Of course, my XML file starts with this: <?xml version="1.0" encoding="utf-8"?>. Although I suppose it is not relevant, I also defined utf-8 for my python script, but since I am not writing unicode characters directly in it, this should not apply here. Same for the following line: from __future__ import unicode_literals which also is right at the beginning.

Next thing I pass the generated Object to my own class where I read tags into variables like this: xmldata.getElementsByTagName(tagName)[0].firstChild.data and assign it to a variable in my class.

Now what perfectly works are those commands (obj is an instance of the class):

for element in obj:
    print element

And this command does work as well:

print obj.__repr__()

I defined __iter__() to just yield every variable while __repr__() uses the typical printf stuff: "%s" % self.varname

Both commands print perfectly and can output the unicode character.
What does not work is this:

print obj

And now I am stuck because this throws the dreaded

UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 47:

So what am I missing? What am I doing wrong? I am looking for a general solution, I always want to handle strings as unicode, just to avoid any possible errors and write a compatible program.

Edit: I also defined this:

def __str__(self):
    return self.__repr__()
def __unicode__(self):
    return self.__repr__()

From documentation I got that this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:41:38+00:00Added an answer on June 6, 2026 at 7:41 pm

    I finally solved it. The problem was (I am not sure why) that if you called either __str__() or __repr__() directly it would be hapyp to handle it well, but printing it directly (as in: print obj) does not work (although it should only just call __str__() itself).

    The final help came from this article. I already got to the step where I got it to print to the console (but a wrong letter) when I used utf-8 encoding. Finally solved it to be perfectly correct by defining this:

    def __str__(self):
        return self.__repr__().encode(stdout.encoding)
    

    Now the only open question that remains is: Why do print obj.__str__() and print obj differently with this? It does make no sense to me. And yes, to stress that again: Calling the former or __repr__() DID work. And still does with the explicit encoding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read from this article http://codahale.com/how-to-safely-store-a-password/ and it says using a salt isn't
I have read from the question How to gain access to a ServletContext instance
How to read Unicode file in php. I have used file_get_contents() function, but after
I'm testing my application as grails run-app. I have read How to remove app
I have read all the documentation I found on how to integrate Unity3D projects
I have read a ton of articles about how to store and compare entered
I have read offline-access-removal , how to handle expired tokens and How to extend
I have read the GtkSharp TreeView tutorial wherein the author describes how to setup
Am tearing my hair out here. Have read many examples which describe how to
I have recently read topics about memory fragmentation: How to solve Memory Fragmentation and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.