Skip to content Skip to sidebar Skip to footer

Elementtree Will Not Parse Special Characters With Python 2.7

I had to rewrite my python script from python 3 to python2 and after that I got problem parsing special characters with ElementTree. This is a piece of my xml:

The first difference is one you're probably already aware of: Python's print statement in version 2 became a print function in version 3. That change is creating a special circumstance in your case, which I'll get to a little later. But briefly, this is the difference in how 'print' works:

In Python 3:

>>># Two arguments 'Hi' and 'there' get passed to the function 'print'.>>># They are concatenated with a space separator and printed.>>>print('Hi', 'there') >>>Hi there

In Python 2:

>>># 'print' is a statement which doesn't need parenthesis.>>># The parenthesis instead create a tuple containing two elements >>># 'Hi' and 'there'. This tuple is then printed.>>>print('Hi', 'there')>>>('Hi', 'there')

The second problem in your case is that tuples print themselves by calling repr() on each of their elements. In Python 3, repr() displays unicode as you want. But in Python 2, repr() uses escape characters for any byte values which fall outside the printable ASCII range (e.g., larger than 127). This is why you're seeing them.

You may decide to resolve this issue, or not, depending on what you're goal is with your code. The representation of a tuple in Python 2 uses escape characters because it's not designed to be displayed to an end-user. It's more for your internal convenience as a developer, for troubleshooting and similar tasks. If you're simply printing it for yourself, then you may not need to change a thing because Python is showing you that the encoded bytes for that non-ASCII character are correctly there in your string. If you do want to display something to the end-user which has the format of how tuples look, then one way to do it (which retains correct printing of unicode) is to manually create the formatting, like this:

def printAccountPlan(xmltree):
    data = (i.attrib['number'], i.attrib['type'], i.text)
    print "('account:', '%s', 'AccountType:', '%s', 'Name:', '%s')" % data
# Produces this:
# ('account:', '89890000', 'AccountType:', 'Kostnad', 'Name:', 'Avsättning egenavgifter')

Post a Comment for "Elementtree Will Not Parse Special Characters With Python 2.7"