Writing Unicode text to a text file?

前端 未结 8 2051
眼角桃花
眼角桃花 2020-11-22 16:46

I\'m pulling data out of a Google doc, processing it, and writing it to a file (that eventually I will paste into a Wordpress page).

It has some non-ASCII symbols. H

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 17:41

    That error arises when you try to encode a non-unicode string: it tries to decode it, assuming it's in plain ASCII. There are two possibilities:

    1. You're encoding it to a bytestring, but because you've used codecs.open, the write method expects a unicode object. So you encode it, and it tries to decode it again. Try: f.write(all_html) instead.
    2. all_html is not, in fact, a unicode object. When you do .encode(...), it first tries to decode it.

提交回复
热议问题