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 7776353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:51:43+00:00 2026-06-01T17:51:43+00:00

Assume I have a document which uses Unicode in tag names, as for example

  • 0

Assume I have a document which uses Unicode in tag names, as for example <año>2012</año>.

When I use etree from lxml to parse such a document, I have no problems, the tree is correctly built. But when (for debugging purposes) I try to print some elements, I get an exception about a failed attempt to encode as ASCII some unicode char.

Is not a problem of terminal configuration or bad encoding of the file, since I can print without problem the name of the node (.tag), which contains the same unicode char. Apparently the problem is caused by the “stringification” of the Element object, which assumes that the tag names are aways plain ascii.

The following code shows the problem (and also shows that it is not a file/terminal/encoding problem).

# coding: utf-8
from lxml import etree
doc = """<?xml version="1.0" encoding="utf-8"?>
<año>2012</año>
"""
x = etree.fromstring(doc)   # No problem
print x.tag                 # No problem
print x                     # Exception

Running the above script in a terminal with a properly defined LC_CTYPE, produces the following output:

año
Traceback (most recent call last):
  File "procesar.py", line 8, in <module>
    print x
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 10: ordinal not in range(128)

Note how print x.tag outputs correctly año. Shouldn’t print x produce something like <Element año at b7d26eb4>?

Is this a known problem? Any ideas about workarounds?

  • 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-01T17:51:44+00:00Added an answer on June 1, 2026 at 5:51 pm

    You have to transform unicode strings into byte strings before output

    Try:

    print unicode(x).encode('utf8')
    

    quoting the unicode function:

    For objects which provide a __unicode__() method, it will call this method without arguments to create a Unicode string. For all other objects, the 8-bit string version or representation is requested and then converted to a Unicode string using the codec for the default encoding in ‘strict’ mode.

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

Sidebar

Related Questions

Assume you have a list item, <li id=foo> which you want to fade from
Assume you have a file which has been committed in your Git repo. You
Within document.ready, I store a selector in a variable (which I assume once evaluated,
Assume / is the document root of my domain example.com . /.htaccess RewriteEngine on
I have an XML schema and an instance document I get from a customer.
Lets assume we have an entity called Document and an other one called User.
Assume I have a C# class like this: [XmlRoot(floors)] public class FloorCollection { [XmlElement(floor)]
Assume I have the following string: Hellotoevryone<img height=115 width=150 alt= src=/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg />Iamsogladtoseeall. This
Assume I have access to a SMB server at IP 1.2.3.4, how can I
Assume I have a application that stores data,gets data and processes data and stores

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.