Python 如何在 CSV 文件中写入 UTF-8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18766955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:45:59  来源:igfitidea点击:

How to write UTF-8 in a CSV file

pythoncsvencodingutf-8

提问by Martin

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 characters. I use following code:

我正在尝试从 PyQt4 中创建一个 csv 格式的文本文件QTableWidget。我想用 UTF-8 编码编写文本,因为它包含特殊字符。我使用以下代码:

import codecs
...
myfile = codecs.open(filename, 'w','utf-8')
...
f = result.table.item(i,c).text()
myfile.write(f+";")

It works until the cell contains a special character. I tried also with

它一直工作到单元格包含特殊字符为止。我也试过

myfile = open(filename, 'w')
...
f = unicode(result.table.item(i,c).text(), "utf-8")

But it also stops when a special character appears. I have no idea what I am doing wrong.

但是当出现特殊字符时它也会停止。我不知道我做错了什么。

回答by Aaron Digulla

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

Python 文档中的示例展示了如何编写 Unicode CSV 文件:http: //docs.python.org/2/library/csv.html#examples

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

(这里不能复制代码,因为它受版权保护)

回答by Gijs

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

使用这个包,它就可以工作:https://github.com/jdunck/python-unicodecsv

回答by kqw

From your shell run:

从你的 shell 运行:

pip2 install unicodecsv

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

而且(不同于原来的问题)假设你正在使用Python的内置csv模块,转
import csv
import unicodecsv as csv在你的代码。

回答by Zanon

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

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.

对于 Python 2.x,请看这里

回答by vpathak

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

一个非常简单的技巧是使用 json 导入而不是 csv。例如,而不是 csv.writer 只需执行以下操作:

    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.*

基本上,给定正确顺序的字段列表,json 格式的字符串与 csv 行相同,除了 [ 和 ] 分别位于开头和结尾。json 似乎对 python 2.* 中的 utf-8 很健壮

回答by Bojan Bogdanovic

For me the UnicodeWriterclass from Python 2 CSV module documentation didn't really work as it breaks the csv.writer.write_row()interface.

对我来说UnicodeWriter,Python 2 CSV 模块文档中的类并没有真正起作用,因为它破坏了csv.writer.write_row()界面。

For example:

例如:

csv_writer = csv.writer(csv_file)
row = ['The meaning', 42]
csv_writer.writerow(row)

works, while:

有效,同时:

csv_writer = UnicodeWriter(csv_file)
row = ['The meaning', 42]
csv_writer.writerow(row)

will throw AttributeError: 'int' object has no attribute 'encode'.

会扔AttributeError: 'int' object has no attribute 'encode'

As UnicodeWriterobviously expects all column values to be strings, we can convert the values ourselves and just use the default CSV module:

作为UnicodeWriter显然希望所有列的值是字符串,我们可以将这些值自己和只使用默认CSV模块:

def to_utf8(lst):
    return [unicode(elem).encode('utf-8') for elem in lst]

...
csv_writer.writerow(to_utf8(row))

Or we can even monkey-patch csv_writer to add a write_utf8_rowfunction - the exercise is left to the reader.

或者我们甚至可以修补 csv_writer 来添加一个write_utf8_row函数——这个练习留给读者。

回答by pymen

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

对于python2,您可以在代码之前使用此代码csv_writer.writerows(rows)
此代码不会将整数转换为 utf-8 字符串

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