파이썬에서 utf-8 csv 파일을 만들 수 없습니다.파이썬에서 utf-8 csv 파일 만들기
나는 그것이 문서를의 읽으려고하고, examples section에, 그것은 말한다 :
For all other encodings the following UnicodeReader and UnicodeWriter classes can be used. They take an additional encoding parameter in their constructor and make sure that the data passes the real reader or writer encoded as UTF-8:
좋아. 그래서 나는이 코드를 가지고 :
values = (unicode("Ñ", "utf-8"), unicode("é", "utf-8"))
f = codecs.open('eggs.csv', 'w', encoding="utf-8")
writer = UnicodeWriter(f)
writer.writerow(values)
을 그리고이 오류가 점점 계속 : 그래서 나는 모든 설정 때문에 도대체 내가 뭘 잘못 이해할 수
line 159, in writerow
self.stream.write(data)
File "/usr/lib/python2.6/codecs.py", line 686, in write
return self.writer.write(data)
File "/usr/lib/python2.6/codecs.py", line 351, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 22: ordinal not in range(128)
누군가가 나에게 빛을주지하시기 바랍니다 수를 UnicodeWriter 클래스를 호출하기 전에 어디에서나 인코딩 할 수 있습니까?
class UnicodeWriter:
"""
A CSV writer which will write rows to CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
# Redirect output to a queue
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()
def writerow(self, row):
self.writer.writerow([s.encode("utf-8") for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = self.encoder.encode(data)
# write to the target stream
self.stream.write(data)
# empty queue
self.queue.truncate(0)
def writerows(self, rows):
for row in rows:
self.writerow(row)
그것은 문제가 seens 코덱 열기. 내가 그것을 제거하고 그냥 열어 사용하면 작동합니다. 왜? –