0

I got this error while writing to a file like this: f.write(str(data)) and the problem was I was calling str on some data that contained Unicode characters and the default "ascii" encoder does not know how to encode them to bytes. This I understand.

Then I did print unicode(data)[13:20] to see the offending characters and I could not see anything. Is there a way to detect that ? Thanks.

Based on Steven Kryskala's suggestion I did print repr(unicode(data))[13:21] and got this:

u'\n<body s'

Whats bad about that ?

Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208

1 Answers1

0

replace with this try this

    f.write(data.encode())
  • data is an object of a class. It does not have `encode()` function like strings. – Ankur Agarwal May 22 '15 at 23:20
  • what is the source of data?? you cannot write an object of a class directly you have to define some function to serealize it or use pickle etc could you edit your question and paste your code please? – Narcisse Doudieu Siewe May 22 '15 at 23:24