1

I am working on an openstreetmap project and have the following code

class UnicodeDictWriter(csv.DictWriter, object):  
            """Extend csv.DictWriter to handle Unicode input"""  

    def writerow(self, row):  
        super(UnicodeDictWriter, self).writerow({
        k: (v.encode('utf-8') if isinstance(v, unicode) else v) for k, v in row.items()})  

    def writerows(self, rows):  
         for row in rows:  
             self.writerow(row)  

It throws me the error message name unicode is not defined, research did not give a clue for solving it. How do I modify the code to a working one? (Please be patient, I am still learning)

1 Answer 1

1

It look like

isinstance(v, unicode)

appears to be a Python2 way for checking whether or not the object is unicode. With Python3, you have bytes as an instance, so try using

not isinstance(v, bytes)

instead.

Sign up to request clarification or add additional context in comments.

1 Comment

bytes and unicode are opposites of each other; in order to be anywhere remotely equivalent, the second snippet should be not isinstance(v, bytes).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.