How to write UTF-8 in a CSV file

前端 未结 7 1541
一生所求
一生所求 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: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
    

提交回复
热议问题