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


Groups > comp.lang.python > #41789

Re: Separate Rows in reader

Newsgroups comp.lang.python
Date 2013-03-24 08:57 -0700
References <fdcdb6d8-c63a-4909-8b56-13204a0d474c@googlegroups.com> <mailman.3662.1364104023.2939.python-list@python.org> <5a1660c3-ca17-452c-ae0f-ee61f4319a8f@googlegroups.com> <514EF9A3.2000603@davea.name> <mailman.3668.1364132867.2939.python-list@python.org>
Message-ID <ef3267b4-ea6a-48c2-b9fa-acca671ae3d1@u5g2000pbs.googlegroups.com> (permalink)
Subject Re: Separate Rows in reader
From rusi <rustompmody@gmail.com>

Show all headers | View raw


On Mar 24, 6:49 pm, Tim Chase <python.l...@tim.thechases.com> wrote:
> On 2013-03-24 09:03, Dave Angel wrote:
>
> > >> <SNIP all those quoted lines doubled by anti-social googlegroups>
>
> [THANK YOU!]
>
> > > Sorry my typo in the output here is the correct output that i
> > > need :
>
> > > [('John Konon', 'Ministry of moon Walks', '4567882', '27-Feb'),
> > > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
>
> > > the difference is that i need a [(row two), (row three), (row
> > > fouth)] . I do not want to display row one which is ['Name', '
> > > Address', 'Telephone', 'Birthday']
>
> > I had to add a delimiter= to accomodate the fact that your data is
> > tab- separated.
>
> Since it has headers, it might make more sense for readability to use
> a DictReader:
>
>   f = file("source.txt", "rb")
>   r = csv.DictReader(f, delimiter='\t')
>   for row in r:
>     print row["Name"], row["Address"], ...
>
> However, if it's just stripping off the first row and operating
> directly on the resulting data, then I'd just be tempted to do
>
>   r = csv.reader(f, delimiter='\t')
>   r.next() # discard the headers, or optionally store them
>   for row in r:
>     ...
>
> -tkc

After doing:

>>> import csv
>>> original = file('friends.csv', 'rU')
>>> reader = csv.reader(original, delimiter='\t')


Stripping of the first line is:
>>> list(reader)[1:]

If for some reason you want to tuplify each row do either:
>>> [tuple(row) for row in list(reader)[1:]]

Or else
>>> map(tuple,list(reader)[1:])

I usually prefer comprehensions to maps. Here though, the map is
shorter. Your choice


Then you can of course make your code more performant thus:
>>> reader.next()
>>> (tuple(row) for row in reader)

In the majority of cases this optimization is not worth it
In any case, strewing prints all over the code is a bad habit (except
for debugging).

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


Thread

Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-23 22:20 -0700
  Re: Separate Rows in reader Dave Angel <davea@davea.name> - 2013-03-24 01:46 -0400
    Re: Separate Rows in reader rusi <rustompmody@gmail.com> - 2013-03-24 00:34 -0700
      Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-24 01:18 -0700
    Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-24 01:11 -0700
      Re: Separate Rows in reader Dave Angel <davea@davea.name> - 2013-03-24 09:03 -0400
      Re: Separate Rows in reader Tim Chase <python.list@tim.thechases.com> - 2013-03-24 08:49 -0500
        Re: Separate Rows in reader rusi <rustompmody@gmail.com> - 2013-03-24 08:57 -0700
          Re: Separate Rows in reader Tim Chase <python.list@tim.thechases.com> - 2013-03-24 13:28 -0500
            Re: Separate Rows in reader rusi <rustompmody@gmail.com> - 2013-03-24 19:08 -0700
    Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-24 01:11 -0700
  Re: Separate Rows in reader ypsun <winter0128@gmail.com> - 2013-03-24 03:58 -0700
  Re: Separate Rows in reader ypsun <winter0128@gmail.com> - 2013-03-24 04:10 -0700
    Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-24 23:52 -0700
      Re: Separate Rows in reader rusi <rustompmody@gmail.com> - 2013-03-25 06:51 -0700
        Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-25 18:05 -0700
          Re: Separate Rows in reader Dave Angel <davea@davea.name> - 2013-03-25 21:40 -0400
            Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-25 20:33 -0700
              Re: Separate Rows in reader MRAB <python@mrabarnett.plus.com> - 2013-03-26 03:48 +0000
                Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-26 00:24 -0700
                Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-26 00:24 -0700
                Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-27 02:35 -0700
                Re: Separate Rows in reader rusi <rustompmody@gmail.com> - 2013-03-27 04:18 -0700
                Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-27 15:12 -0700
                Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-27 15:26 -0700
                Re: Separate Rows in reader rusi <rustompmody@gmail.com> - 2013-03-27 18:24 -0700
                Re: Separate Rows in reader Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-28 01:32 +0000
                Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-27 02:35 -0700
                Re: Separate Rows in reader Tim Roberts <timr@probo.com> - 2013-03-28 21:28 -0700
            Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-25 20:33 -0700
  Re: Separate Rows in reader Jiewei Huang <jiewei24@gmail.com> - 2013-03-24 18:15 -0700

csiph-web