Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:text': 0.05; 'binary': 0.07; 'subject:file': 0.07; 'string': 0.09; 'iterate': 0.09; 'lawrence': 0.09; 'newline': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'python': 0.11; 'csv': 0.16; 'expects': 0.16; 'iterator': 0.16; 'mode,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'module': 0.19; 'version.': 0.19; 'examples': 0.20; 'platforms': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'fine': 0.24; 'file.': 0.24; "i've": 0.25; 'handling': 0.26; 'mention': 0.26; 'header:X-Complaints-To:1': 0.27; '[1]': 0.29; 'tim': 0.29; 'mode': 0.30; 'especially': 0.30; 'lines': 0.31; 'that.': 0.31; 'chase': 0.31; 'file': 0.32; 'open': 0.33; 'url:python': 0.33; 'reader': 0.33; 'objects': 0.35; 'object,': 0.36; 'method': 0.36; 'url:org': 0.36; 'list': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; 'anything': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'read': 0.60; '2.7.': 0.84; 'difference.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Neil Cerutti Subject: Re: parse a csv file into a text file Date: Thu, 6 Feb 2014 18:34:55 +0000 (UTC) Organization: Norwich University References: <5c268845-003f-4e24-b27a-c89e9fbfcc6c@googlegroups.com> <20140206115116.69553ffe@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jackman.norwich.edu User-Agent: slrn/0.9.9p1/mm/ao (Win32) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391711726 news.xs4all.nl 2867 [2001:888:2000:d::a6]:58808 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65549 On 2014-02-06, Tim Chase wrote: > On 2014-02-06 17:40, Mark Lawrence wrote: >> On 06/02/2014 14:02, Neil Cerutti wrote: >> > >> > You must open the file in binary mode, as that is what the csv >> > module expects in Python 2.7. newline handling can be enscrewed >> > if you forget. >> > >> > file = open('raw.csv', 'b') >> > >> >> I've never opened a file in binary mode to read with the csv module >> using any Python version. Where does it state that you must do >> this? > > While the docs don't currently say anything about it, all the > examples at [1] use 'rb' or 'wb' when opening the file. I've > long wondered about that. Especially as I've passed non-file > objects like lists/iterators to the csv.reader/csv.DictReader > and had them work just fine (and would be a little perturbed if > they broke). They do actually mention it. From: http://docs.python.org/2/library/csv.html csv.reader(csvfile, dialect='excel', **fmtparams) Return a reader object which will iterate over lines in the given csvfile. csvfile can be any object which supports the iterator protocol and returns a string each time its next() method is called — file objects and list objects are both suitable. If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference. So it's stipulated only for file objects on systems where it might make a difference. -- Neil Cerutti