2012-08-09 8 views
28

[python web site] [1]からこのスクリプトをコピーしました。今のエンコードに問題:私はこれを実行したときにpython csv unicode 'ascii'コーデックは、位置1の文字 'ufxf6'をエンコードできません:序数が範囲内にありません(128)

import sqlite3 
import csv 
import codecs 
import cStringIO 
import sys 

class UTF8Recoder: 
    """ 
    Iterator that reads an encoded stream and reencodes the input to UTF-8 
    """ 
    def __init__(self, f, encoding): 
     self.reader = codecs.getreader(encoding)(f) 

    def __iter__(self): 
     return self 

    def next(self): 
     return self.reader.next().encode("utf-8") 

class UnicodeReader: 
    """ 
    A CSV reader which will iterate over lines in the CSV file "f", 
    which is encoded in the given encoding. 
    """ 

    def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds): 
     f = UTF8Recoder(f, encoding) 
     self.reader = csv.reader(f, dialect=dialect, **kwds) 

    def next(self): 
     row = self.reader.next() 
     return [unicode(s, "utf-8") for s in row] 

    def __iter__(self): 
     return self 

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) 

エンコーディングで今回の問題、それは私に、このエラーが発生しました:

Traceback (most recent call last): 
    File "makeCSV.py", line 87, in <module> 
    uW.writerow(d) 
    File "makeCSV.py", line 54, in writerow 
    self.writer.writerow([s.encode("utf-8") for s in row]) 
AttributeError: 'int' object has no attribute 'encode' 

は、それから私は、文字列にすべての整数を変換するが、今回は私はこのエラーを得ました:

Traceback (most recent call last): 
    File "makeCSV.py", line 87, in <module> 
    uW.writerow(d) 
    File "makeCSV.py", line 54, in writerow 
    self.writer.writerow([str(s).encode("utf-8") for s in row]) 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 1: ordinal not in range(128) 

私は上記のUnicode文字を処理するために実装しましたが、それは私にそのようなエラーを与えます。問題は何ですか?問題を解決する方法は何ですか?

答えて

64

Then I converted all integers to string,

あなたは両方の整数にバイト文字列から文字列を変換します。文字列の場合、これはデフォルトの文字エンコーディングを使用します。これはASCIIです。非ASCII文字がある場合は失敗します。 strの代わりにunicodeが必要です。

self.writer.writerow([unicode(s).encode("utf-8") for s in row]) 

このメソッドを呼び出す前に、すべてをUnicodeに変換する方がよい場合があります。このクラスは、Unicode文字列を解析するために特別に設計されています。他のデータ型をサポートするようには設計されていませんでした。

関連する問題