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

前端 未结 2 2034
甜味超标
甜味超标 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'))
    
    0 讨论(0)
  • 2021-01-05 14:50

    With Python's csv module, you can write a UTF-8 file that Excel will read correctly if you place a BOM at the beginning of the file.

    with open('myfile.csv', 'wb') as f:
        f.write(u'\ufeff'.encode('utf8'))
        writer = csv.writer(f, delimiter=';', lineterminator='\n', quoting=csv.QUOTE_ALL, dialect='excel')
        ...
    

    The same should work with unicodecsv. I suppose you can write the BOM directly to the HttpResponse object, if not you can use StringIO to first write your file.

    Edit:

    Here's some sample code that writes a UTF-8 CSV file with non-ASCII characters. I'm taking Django out of the equation for simplicity. I can read that file in Excel.

    # -*- coding: utf-8 -*-
    import csv
    import os
    response = open(os.path.expanduser('~/utf8_test.csv'), 'wb')
    response.write(u'\ufeff'.encode('utf8'))
    writer = csv.writer(response, delimiter=';' , dialect='excel')
    writer.writerow(['Second row', 'A', 'B', 'C', '"Testing"', u"ελληνικά".encode('utf8')])
    response.close()
    
    0 讨论(0)
提交回复
热议问题