How to write UTF-8 in a CSV file

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

提交回复
热议问题