0

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
Marcelo
  • 438
  • 5
  • 16

1 Answers1

1

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.

Jonathan March
  • 5,800
  • 2
  • 14
  • 16
  • Thanks Jonathan, I put a try: except into my code. When I ran the code again, guess what? The error disappeared!!! Yesterday, I tried all day long, and I always got the same error. Well, I don't know why, but, at least, it is working now. Man, I love my job! Magic things happen :) – Marcelo Oct 23 '13 at 07:51
  • Glad it's working now. No mystery I think, just an online file that was broken for a while, then fixed. Please mark this answer as the solution, so that your question won't remaining hanging as "unanswered". (Only you can mark it as answered.) This is standard stackoverflow protocol. Thanks! – Jonathan March Oct 24 '13 at 00:54