Django create CSV file that contains Unicode and can be opened directly with Excel

前端 未结 2 2033
甜味超标
甜味超标 2021-01-05 14:21

I want to create a CSV file through Django that contains unicode data (greek characters) and I want it to be opened directly from MS Excel. Elsewhere I had read about the un

2条回答
  •  鱼传尺愫
    2021-01-05 14:36

    I've never been able to open a UTF-8-encoded (CSV) file in Excel. The only way I managed to make Excel import files properly was with UTF-16LE. YMMV.

    EDIT

    First

    writer.writerow(codecs.BOM_UTF16_LE)
    

    Then (as many times as required; str is the string to encode and write)

    writer.writerow(str.decode('utf8').encode('utf_16_le'))
    

提交回复
热议问题