How to write UTF-8 in a CSV file

前端 未结 7 1477
一生所求
一生所求 2020-11-27 03:30

I am trying to create a text file in csv format out of a PyQt4 QTableWidget. I want to write the text with a UTF-8 encoding because it contains special characte

相关标签:
7条回答
  • 2020-11-27 03:44

    A very simple hack is to use the json import instead of csv. For example instead of csv.writer just do the following:

        fd = codecs.open(tempfilename, 'wb', 'utf-8')  
        for c in whatever :
            fd.write( json.dumps(c) [1:-1] )   # json dumps writes ["a",..]
            fd.write('\n')
        fd.close()
    

    Basically, given the list of fields in correct order, the json formatted string is identical to a csv line except for [ and ] at the start and end respectively. And json seems to be robust to utf-8 in python 2.*

    0 讨论(0)
  • 2020-11-27 03:46

    From your shell run:

    pip2 install unicodecsv
    

    And (unlike the original question) presuming you're using Python's built in csv module, turn
    import csv into
    import unicodecsv as csv in your code.

    0 讨论(0)
  • 2020-11-27 03:50

    Use this package, it just works: https://github.com/jdunck/python-unicodecsv.

    0 讨论(0)
  • 2020-11-27 03:50

    For python2 you can use this code before csv_writer.writerows(rows)
    This code will NOT convert integers to utf-8 strings

    def encode_rows_to_utf8(rows):
        encoded_rows = []
        for row in rows:
            encoded_row = []
            for value in row:
                if isinstance(value, basestring):
                    value = unicode(value).encode("utf-8")
                encoded_row.append(value)
            encoded_rows.append(encoded_row)
        return encoded_rows
    
    0 讨论(0)
  • 2020-11-27 03:58

    It's very simple for Python 3.x (docs).

    import csv
    
    with open('output_file_name', 'w', newline='', encoding='utf-8') as csv_file:
        writer = csv.writer(csv_file, delimiter=';')
        writer.writerow('my_utf8_string')
    

    For Python 2.x, look here.

    0 讨论(0)
  • 2020-11-27 04:01

    The examples in the Python documentation show how to write Unicode CSV files: http://docs.python.org/2/library/csv.html#examples

    (can't copy the code here because it's protected by copyright)

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