Canopy UnicodeEncodeError: 'ascii' codec can't encode characters in position 31-32: ordinal not in range(128)

StackOverflow https://stackoverflow.com/questions/19527826

Question

I saw other questions with the same title, but they don't answer what I'm looking for. I have the native python 2.7.2 in my macbook OS X (mountain lion), and use the package RDFlib with no problem. Few days ago, I installed Canopy, which is a nice suite, and tried to run the same code parsing an RDF data in http://dbpedia.org and extracting only the literals from it. As the same code works in pure python, I infer that it might be something with Canopy. Does anyone know how to solve this problem?

Here is the code:

import rdflib
graph = rdflib.Graph()
graph.parse("http://dbpedia.org/resource/Johann_Sebastian_Bach")
output = []
for s, p, o in graph:
    if type(o) == rdflib.term.Literal:
        if o.language=='en' or o.language=='' :
            output.append(o.toPython()) #creating file

for t in output:
    print t
Was it helpful?

Solution

At first I was able to reproduce your error, then the error disappeared (even with clean start). I suspect bad data that was cleaned up today. I suggest that you put in a try: except inside your loop, to catch and handle UnicodeEncodeError when it does occur.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top