Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65549

Re: parse a csv file into a text file

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 <python-python-list@m.gmane.org>
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 <neilc@norwich.edu>
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> <ld04ln$6h0$2@ger.gmane.org> <ld0he1$dfb$1@ger.gmane.org> <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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6453.1391711723.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 2014-02-06, Tim Chase <python.list@tim.thechases.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

parse a csv file into a text file Zhen Zhang <zhen.zhang.uoft@gmail.com> - 2014-02-05 16:10 -0800
  Re: parse a csv file into a text file Asaf Las <roegltd@gmail.com> - 2014-02-05 16:17 -0800
    Re: parse a csv file into a text file Zhen Zhang <zhen.zhang.uoft@gmail.com> - 2014-02-05 23:56 -0800
  Re: parse a csv file into a text file Roy Smith <roy@panix.com> - 2014-02-05 19:33 -0500
    Re: parse a csv file into a text file Zhen Zhang <zhen.zhang.uoft@gmail.com> - 2014-02-05 23:52 -0800
      Re: parse a csv file into a text file Asaf Las <roegltd@gmail.com> - 2014-02-06 00:15 -0800
        Re: parse a csv file into a text file Asaf Las <roegltd@gmail.com> - 2014-02-06 00:48 -0800
      Re: parse a csv file into a text file MRAB <python@mrabarnett.plus.com> - 2014-02-06 13:16 +0000
        Re: parse a csv file into a text file Rustom Mody <rustompmody@gmail.com> - 2014-02-06 05:20 -0800
  Re: parse a csv file into a text file MRAB <python@mrabarnett.plus.com> - 2014-02-06 00:34 +0000
    Re: parse a csv file into a text file Zhen Zhang <zhen.zhang.uoft@gmail.com> - 2014-02-06 00:01 -0800
  Re: parse a csv file into a text file Tim Chase <python.list@tim.thechases.com> - 2014-02-05 18:46 -0600
    Re: parse a csv file into a text file Asaf Las <roegltd@gmail.com> - 2014-02-05 19:59 -0800
      Re: parse a csv file into a text file Tim Chase <python.list@tim.thechases.com> - 2014-02-05 22:09 -0600
        Re: parse a csv file into a text file Asaf Las <roegltd@gmail.com> - 2014-02-05 20:17 -0800
    Re: parse a csv file into a text file Zhen Zhang <zhen.zhang.uoft@gmail.com> - 2014-02-06 00:07 -0800
      Re: parse a csv file into a text file Tim Chase <python.list@tim.thechases.com> - 2014-02-06 12:49 -0600
  Re: parse a csv file into a text file Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-06 00:50 +0000
  Re:parse a csv file into a text file Dave Angel <davea@davea.name> - 2014-02-05 19:57 -0500
    Re: parse a csv file into a text file Zhen Zhang <zhen.zhang.uoft@gmail.com> - 2014-02-06 00:12 -0800
      Re: parse a csv file into a text file Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-02-06 10:49 +0200
      Re: parse a csv file into a text file Dave Angel <davea@davea.name> - 2014-02-06 06:35 -0500
      Re: parse a csv file into a text file Dave Angel <davea@davea.name> - 2014-02-06 06:53 -0500
  Re: parse a csv file into a text file Terry Reedy <tjreedy@udel.edu> - 2014-02-05 22:01 -0500
  Re: parse a csv file into a text file Neil Cerutti <neilc@norwich.edu> - 2014-02-06 14:02 +0000
  Re: parse a csv file into a text file Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-06 17:40 +0000
  Re: parse a csv file into a text file Tim Chase <python.list@tim.thechases.com> - 2014-02-06 11:51 -0600
  Re: parse a csv file into a text file Tim Golden <mail@timgolden.me.uk> - 2014-02-06 18:05 +0000
  Re: parse a csv file into a text file Neil Cerutti <neilc@norwich.edu> - 2014-02-06 18:34 +0000
  Re: parse a csv file into a text file Tim Chase <python.list@tim.thechases.com> - 2014-02-06 12:51 -0600

csiph-web