2.7 CSV module wants unicode, but doesn't want unicode

后端 未结 3 1634
余生分开走
余生分开走 2021-02-19 08:01
csvfile_ = open(finishedFileName+num+\".csv\",\"w\",newline=\'\')
writ = csv.writer(csvfile_, dialect=\'excel\')
firstline = unicode(str(firstline))
try:
    writ.writer         


        
3条回答
  •  清酒与你
    2021-02-19 08:25

    Unfortunately, 3to2 used the io.open() call instead of the built-in Python 2 open() function. This opened the file in text mode, which like on Python 3 expects Unicode input.

    However, the csv module does not support Unicode data; it certainly does not produce Unicode.

    You'll either have to open the file in binary mode on Python 2:

    mode = 'w'
    if sys.version_info.major < 3:
        mode += 'b'
    csvfile_ = open(finishedFileName + num + ".csv", mode, newline='')
    

    or use the built-in open() call instead:

    csvfile_ = open(finishedFileName + num + ".csv", 'wb')
    

    where you have to use 'wb' as the mode anyway.

    If you are trying to write out unicode data, you'll have to encode that data before passing it to the csv.writer() object. The csv module examples section includes code to make encoding from Unicode before writing a little easier.

提交回复
热议问题