CSV, DictWriter, unicode and utf-8

后端 未结 1 1304
一生所求
一生所求 2021-02-10 12:49

I am having problems with the DictWriter and non-ascii characters. A short version of my problem:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import codecs
im         


        
相关标签:
1条回答
  • 2021-02-10 13:07

    The object you obtain with codecs.open wants a unicode string in its write method -- that's the whole point. csv.DictWriter of course is calling that method with a utf8-encoded byte string instead, whence the exception.

    Change f's creation to f = open("test.csv", 'wb') (taking codecs out of the picture) and things should work just fine.

    0 讨论(0)
提交回复
热议问题