Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The example code of how to read Unicode given at <a href="http://docs.python.org/library/csv.html#examples" rel="noreferrer">http://docs.python.org/library/csv.html#examples</a> looks to be obsolete, as it doesn't work with Python 2.6 and 2.7.</p> <p>Here follows <code>UnicodeDictReader</code> which works with utf-8 and may be with other encodings, but I only tested it on utf-8 inputs.</p> <p>The idea in short is to decode Unicode only after a csv row has been split into fields by <code>csv.reader</code>.</p> <pre><code>class UnicodeCsvReader(object): def __init__(self, f, encoding="utf-8", **kwargs): self.csv_reader = csv.reader(f, **kwargs) self.encoding = encoding def __iter__(self): return self def next(self): # read and split the csv row into fields row = self.csv_reader.next() # now decode return [unicode(cell, self.encoding) for cell in row] @property def line_num(self): return self.csv_reader.line_num class UnicodeDictReader(csv.DictReader): def __init__(self, f, encoding="utf-8", fieldnames=None, **kwds): csv.DictReader.__init__(self, f, fieldnames=fieldnames, **kwds) self.reader = UnicodeCsvReader(f, encoding=encoding, **kwds) </code></pre> <p>Usage (source file encoding is utf-8):</p> <pre><code>csv_lines = ( "абв,123", "где,456", ) for row in UnicodeCsvReader(csv_lines): for col in row: print(type(col), col) </code></pre> <p>Output:</p> <pre><code>$ python test.py &lt;type 'unicode'&gt; абв &lt;type 'unicode'&gt; 123 &lt;type 'unicode'&gt; где &lt;type 'unicode'&gt; 456 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload