Note that there are some explanatory texts on larger screens.

plurals
  1. POUnicodeEncodeError when parsing XML using cElementTree within Applescript
    primarykey
    data
    text
    <p>Apologies if this is a duplicate or something really obvious, but please bear with me as I'm new to Python. I'm trying to use cElementTree (Python 2.7.5) to parse an XML file within Applescript. The XML file contains some fields with non-ASCII text encoded as entities, such as <code>&lt;foo&gt;caf&amp;#233;&lt;/foo&gt;</code>. </p> <p>Running the following basic code in Terminal outputs pairs of tags and tag contents as expected:</p> <pre><code>import xml.etree.cElementTree as etree parser = etree.XMLParser(encoding="utf-8") tree = etree.parse("myfile.xml", parser=parser) root = tree.getroot() for child in root: print child.tag, child.text </code></pre> <p>But when I run that same code from within Applescript using <code>do shell script</code>, I get the dreaded <strong>UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 10: ordinal not in range(128)</strong>.</p> <p>I found that if I change my <code>print</code> line to </p> <pre><code> print [child.tag, child.text] </code></pre> <p>then I do get a string containing XML tag/value pairs wrapped in [''], but any non-ASCII characters then get passed onto Applescript as the literal Unicode character string (so I end up with <code>u'caf\\xe9'</code>). </p> <p>I tried a couple of things, including a.) reading the .xml file into a string and using .fromstring instead of .parse, b.) trying to convert the .xml file to str before importing it into cElementTree, c.) just sticking .encode wherever I could to see if I could avoid the ASCII codec, but no solution yet. I'm stuck using Applescript as a container, unfortunately. Thanks in advance for advice!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload