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


Groups > comp.lang.python > #41767 > unrolled thread

Separate Rows in reader

Started byJiewei Huang <jiewei24@gmail.com>
First post2013-03-23 22:20 -0700
Last post2013-03-24 18:15 -0700
Articles 20 on this page of 31 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  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

Page 1 of 2  [1] 2  Next page →


#41767 — Separate Rows in reader

FromJiewei Huang <jiewei24@gmail.com>
Date2013-03-23 22:20 -0700
SubjectSeparate Rows in reader
Message-ID<fdcdb6d8-c63a-4909-8b56-13204a0d474c@googlegroups.com>
Hi all,

Currently create a simple text-based database of information about people
 

I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
Name	 Address	Telephone	Birthday  
John Konon	Ministry of Moon Walks	4567882	27-Feb
Stacy Kisha	Ministry of Man Power	1234567	17-Jan


My codes are :
import csv
original = file('friends.csv', 'rU')
reader = csv.reader(original)

for row in reader:
    
    print row


and the output is :
['Name', ' Address', 'Telephone', 'Birthday']
['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']

But i wanted to make it

[('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]

can someone show me guidance to this issue

Thanks all

[toc] | [next] | [standalone]


#41768

FromDave Angel <davea@davea.name>
Date2013-03-24 01:46 -0400
Message-ID<mailman.3662.1364104023.2939.python-list@python.org>
In reply to#41767
On 03/24/2013 01:20 AM, Jiewei Huang wrote:
> Hi all,
>
> Currently create a simple text-based database of information about people
>
>
> I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
> Name	 Address	Telephone	Birthday
> John Konon	Ministry of Moon Walks	4567882	27-Feb
> Stacy Kisha	Ministry of Man Power	1234567	17-Jan
>
>
> My codes are :
> import csv
> original = file('friends.csv', 'rU')
> reader = csv.reader(original)
>
> for row in reader:
>
>      print row
>
>
> and the output is :
> ['Name', ' Address', 'Telephone', 'Birthday']
> ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
> ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']
>
> But i wanted to make it
>
> [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
> ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
>
> can someone show me guidance to this issue
>
> Thanks all
>

There are at least 3 differences to the printouts.  Which difference are 
you concerned with?

1) Moon --> Silly
2) square and round brackets in slightly different places
3) row 0 suppressed

Explain which one, or specify a fourth, and also what you have tried to 
address the question.


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#41770

Fromrusi <rustompmody@gmail.com>
Date2013-03-24 00:34 -0700
Message-ID<c7cfc9c7-5f21-40ee-9126-0afa3c0dd971@8g2000pbm.googlegroups.com>
In reply to#41768
On Mar 24, 10:46 am, Dave Angel <da...@davea.name> wrote:
> On 03/24/2013 01:20 AM, Jiewei Huang wrote:
>
>
>
>
>
>
>
>
>
> > Hi all,
>
> > Currently create a simple text-based database of information about people
>
> > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
> > Name        Address        Telephone       Birthday
> > John Konon Ministry of Moon Walks  4567882 27-Feb
> > Stacy Kisha        Ministry of Man Power   1234567 17-Jan
>
> > My codes are :
> > import csv
> > original = file('friends.csv', 'rU')
> > reader = csv.reader(original)
>
> > for row in reader:
>
> >      print row
>
> > and the output is :
> > ['Name', ' Address', 'Telephone', 'Birthday']
> > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
> > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']
>
> > But i wanted to make it
>
> > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
> > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
>
> > can someone show me guidance to this issue
>
> > Thanks all
>
> There are at least 3 differences to the printouts.  Which difference are
> you concerned with?
>
> 1) Moon --> Silly
> 2) square and round brackets in slightly different places
> 3) row 0 suppressed
>
> Explain which one, or specify a fourth, and also what you have tried to
> address the question.
>
> --
> DaveA

Besides does it work as you are describing?
ie your input appears to be tab delimited, whereas the default is ','

[toc] | [prev] | [next] | [standalone]


#41774

FromJiewei Huang <jiewei24@gmail.com>
Date2013-03-24 01:18 -0700
Message-ID<aa62a2b2-e326-49e3-ab91-df187dc24744@googlegroups.com>
In reply to#41770
On Sunday, March 24, 2013 5:34:03 PM UTC+10, rusi wrote:
> On Mar 24, 10:46 am, Dave Angel <da...@davea.name> wrote:
> 
> > On 03/24/2013 01:20 AM, Jiewei Huang wrote:
> 
> >
> 
> >
> 
> >
> 
> >this is the data in csv file http://imgur.com/L4qUkGQ
and this is the correct output that i need to get:
[('John Konon', 'Ministry of moon Walks', '4567882', '27-Feb'), 
( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')] 
sorry for the confusion 

> 
> >
> 
> >
> 
> >
> 
> >
> 
> >
> 
> > > Hi all,
> 
> >
> 
> > > Currently create a simple text-based database of information about people
> 
> >
> 
> > > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
> 
> > > Name        Address        Telephone       Birthday
> 
> > > John Konon Ministry of Moon Walks  4567882 27-Feb
> 
> > > Stacy Kisha        Ministry of Man Power   1234567 17-Jan
> 
> >
> 
> > > My codes are :
> 
> > > import csv
> 
> > > original = file('friends.csv', 'rU')
> 
> > > reader = csv.reader(original)
> 
> >
> 
> > > for row in reader:
> 
> >
> 
> > >      print row
> 
> >
> 
> > > and the output is :
> 
> > > ['Name', ' Address', 'Telephone', 'Birthday']
> 
> > > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
> 
> > > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']
> 
> >
> 
> > > But i wanted to make it
> 
> >
> 
> > > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
> 
> > > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
> 
> >
> 
> > > can someone show me guidance to this issue
> 
> >
> 
> > > Thanks all
> 
> >
> 
> > There are at least 3 differences to the printouts.  Which difference are
> 
> > you concerned with?
> 
> >
> 
> > 1) Moon --> Silly
> 
> > 2) square and round brackets in slightly different places
> 
> > 3) row 0 suppressed
> 
> >
> 
> > Explain which one, or specify a fourth, and also what you have tried to
> 
> > address the question.
> 
> >
> 
> > --
> 
> > DaveA
> 
> 
> 
> Besides does it work as you are describing?
> 
> ie your input appears to be tab delimited, whereas the default is ','

[toc] | [prev] | [next] | [standalone]


#41773

FromJiewei Huang <jiewei24@gmail.com>
Date2013-03-24 01:11 -0700
Message-ID<5a1660c3-ca17-452c-ae0f-ee61f4319a8f@googlegroups.com>
In reply to#41768
On Sunday, March 24, 2013 3:46:49 PM UTC+10, Dave Angel wrote:
> On 03/24/2013 01:20 AM, Jiewei Huang wrote:
> 
> > Hi all,
> 
> >
> 
> > Currently create a simple text-based database of information about people
> 
> >
> 
> >
> 
> > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
> 
> > Name	 Address	Telephone	Birthday
> 
> > John Konon	Ministry of Moon Walks	4567882	27-Feb
> 
> > Stacy Kisha	Ministry of Man Power	1234567	17-Jan
> 
> >
> 
> >
> 
> > My codes are :
> 
> > import csv
> 
> > original = file('friends.csv', 'rU')
> 
> > reader = csv.reader(original)
> 
> >
> 
> > for row in reader:
> 
> >
> 
> >      print row
> 
> >
> 
> >
> 
> > and the output is :
> 
> > ['Name', ' Address', 'Telephone', 'Birthday']
> 
> > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
> 
> > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']
> 
> >
> 
> > But i wanted to make it
> 
> >
> 
> > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
> 
> > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
> 
> >
> 
> > can someone show me guidance to this issue
> 
> >
> 
> > Thanks all
> 
> >
> 
> 
> 
> There are at least 3 differences to the printouts.  Which difference are 
> 
> you concerned with?
> 
> 
> 
> 1) Moon --> Silly
> 
> 2) square and round brackets in slightly different places
> 
> 3) row 0 suppressed
> 
> 
> 
> Explain which one, or specify a fourth, and also what you have tried to 
> 
> address the question.
> 
> 
> 
> 
> 
> -- 
> 
> DaveA


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'] 

[toc] | [prev] | [next] | [standalone]


#41779

FromDave Angel <davea@davea.name>
Date2013-03-24 09:03 -0400
Message-ID<mailman.3666.1364130228.2939.python-list@python.org>
In reply to#41773
On 03/24/2013 04:11 AM, Jiewei Huang wrote:
>
>> <SNIP all those quoted lines doubled by anti-social googlegroups>
>>
>
> 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.

The first two code sections address the dropping of row-zero.
The third section changes the square brackets to parens, if that's what 
you really wanted.
The fourth section goes all around the barn to get the linefeeds in the 
right place.  There's still one extra space on the first line, but 
that's an exercise for the reader.

run with CPython 2.7.3

import csv

original = [
"Name	 Address	Telephone	Birthday  ",
"John Konon	Ministry of Moon Walks	4567882	27-Feb",
"Stacy Kisha	Ministry of Man Power	1234567	17-Jan"
]

#original = file('friends.csv', 'rU')
reader = csv.reader(original, delimiter="\t")
for index, row in enumerate(reader):
     if index:
         print row
print "-----------"

reader = csv.reader(original, delimiter="\t")
data = list(reader)[1:]
print data
print "-----------"

reader = csv.reader(original, delimiter="\t")
data = [tuple(row) for row in list(reader)[1:]]
print data
print "-----------"

reader = csv.reader(original, delimiter="\t")
data = [tuple(row) for row in list(reader)[1:]]
print "[",
for row in data[:-1]:
     print str(row) + ","
print str(data[-1]) +"]"
print "-----------"


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#41782

FromTim Chase <python.list@tim.thechases.com>
Date2013-03-24 08:49 -0500
Message-ID<mailman.3668.1364132867.2939.python-list@python.org>
In reply to#41773
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


[toc] | [prev] | [next] | [standalone]


#41789

Fromrusi <rustompmody@gmail.com>
Date2013-03-24 08:57 -0700
Message-ID<ef3267b4-ea6a-48c2-b9fa-acca671ae3d1@u5g2000pbs.googlegroups.com>
In reply to#41782
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).

[toc] | [prev] | [next] | [standalone]


#41800

FromTim Chase <python.list@tim.thechases.com>
Date2013-03-24 13:28 -0500
Message-ID<mailman.3683.1364149622.2939.python-list@python.org>
In reply to#41789
On 2013-03-24 08:57, rusi wrote:
> On Mar 24, 6:49 pm, Tim Chase <python.l...@tim.thechases.com> wrote:
> After doing:
> 
> >>> import csv
> >>> original = file('friends.csv', 'rU')
> >>> reader = csv.reader(original, delimiter='\t')
> 
> 
> Stripping of the first line is:
> >>> list(reader)[1:]
> >>> [tuple(row) for row in list(reader)[1:]]
> >>> map(tuple,list(reader)[1:])

This works for small sources, but slurps all the data into memory.
Because csv.reader is an iterator/generator, it can process huge CSV
files that wouldn't otherwise fit in memory.  By using either
r.next() (or "next(r)" in newer versions), it fetches one record from
the generator, to be discarded/stored as appropriate.


> 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

If the CSV file is large, using the iterator version is usually worth
the small performance penalty, as you don't have to keep the whole
file in memory.  As somebody who regularly deals with 0.5-1GB CSV
files from cellular providers, I speak from experience of having my
machine choke when reading the whole thing in.

> In any case, strewing prints all over the code is a bad habit
> (except for debugging).

Sorry if my print-statements were misinterpreted--I meant them as a
"do what you want with the data here" stand-in (thus the ellipsis).

-tkc


[toc] | [prev] | [next] | [standalone]


#41811

Fromrusi <rustompmody@gmail.com>
Date2013-03-24 19:08 -0700
Message-ID<85038974-11c7-49ba-9112-556b767d8062@ve4g2000pbc.googlegroups.com>
In reply to#41800
On Mar 24, 11:28 pm, Tim Chase <python.l...@tim.thechases.com> wrote:
>
> Sorry if my print-statements were misinterpreted--I meant them as a
> "do what you want with the data here" stand-in (thus the ellipsis).

Heh! I assumed the OP was a noob to whom this was directed (and whose
original had the print statements in loops).

> As somebody who regularly deals with 0.5-1GB CSV
> files from cellular providers, I speak from experience of having my
> machine choke when reading the whole thing in.

If the 'friends.csv' of the OP goes up to 1GB... Now thats gregarious!

[toc] | [prev] | [next] | [standalone]


#41778

FromJiewei Huang <jiewei24@gmail.com>
Date2013-03-24 01:11 -0700
Message-ID<mailman.3665.1364127021.2939.python-list@python.org>
In reply to#41768
On Sunday, March 24, 2013 3:46:49 PM UTC+10, Dave Angel wrote:
> On 03/24/2013 01:20 AM, Jiewei Huang wrote:
> 
> > Hi all,
> 
> >
> 
> > Currently create a simple text-based database of information about people
> 
> >
> 
> >
> 
> > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
> 
> > Name	 Address	Telephone	Birthday
> 
> > John Konon	Ministry of Moon Walks	4567882	27-Feb
> 
> > Stacy Kisha	Ministry of Man Power	1234567	17-Jan
> 
> >
> 
> >
> 
> > My codes are :
> 
> > import csv
> 
> > original = file('friends.csv', 'rU')
> 
> > reader = csv.reader(original)
> 
> >
> 
> > for row in reader:
> 
> >
> 
> >      print row
> 
> >
> 
> >
> 
> > and the output is :
> 
> > ['Name', ' Address', 'Telephone', 'Birthday']
> 
> > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
> 
> > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']
> 
> >
> 
> > But i wanted to make it
> 
> >
> 
> > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
> 
> > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
> 
> >
> 
> > can someone show me guidance to this issue
> 
> >
> 
> > Thanks all
> 
> >
> 
> 
> 
> There are at least 3 differences to the printouts.  Which difference are 
> 
> you concerned with?
> 
> 
> 
> 1) Moon --> Silly
> 
> 2) square and round brackets in slightly different places
> 
> 3) row 0 suppressed
> 
> 
> 
> Explain which one, or specify a fourth, and also what you have tried to 
> 
> address the question.
> 
> 
> 
> 
> 
> -- 
> 
> DaveA


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'] 

[toc] | [prev] | [next] | [standalone]


#41776

Fromypsun <winter0128@gmail.com>
Date2013-03-24 03:58 -0700
Message-ID<c25271b2-9c45-46ca-b502-aeb68448d819@googlegroups.com>
In reply to#41767
Jiewei Huang於 2013年3月24日星期日UTC+1上午6時20分29秒寫道:
> Hi all,
> 
> 
> 
> Currently create a simple text-based database of information about people
> 
>  
> 
> 
> 
> I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
> 
> Name	 Address	Telephone	Birthday  
> 
> John Konon	Ministry of Moon Walks	4567882	27-Feb
> 
> Stacy Kisha	Ministry of Man Power	1234567	17-Jan
> 
> 
> 
> 
> 
> My codes are :
> 
> import csv
> 
> original = file('friends.csv', 'rU')
> 
> reader = csv.reader(original)
> 
> 
> 
> for row in reader:
> 
>     
> 
>     print row
> 
> 
> 
> 
> 
> and the output is :
> 
> ['Name', ' Address', 'Telephone', 'Birthday']
> 
> ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
> 
> ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']
> 
> 
> 
> But i wanted to make it
> 
> 
> 
> [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
> 
> ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
> 
> 
> 
> can someone show me guidance to this issue
> 
> 
> 
> Thanks all

import csv 
original = file('friends.csv', 'rU')
reader = csv.reader(original) 

result = []
for row in reader:
    result.append(tuple(row))
else:
    print result[1:]


Is this what you need?
but this allocates memory and will be a trouble when you have a big table :P
Note that I assume your csv file content are comma seperated.

[toc] | [prev] | [next] | [standalone]


#41777

Fromypsun <winter0128@gmail.com>
Date2013-03-24 04:10 -0700
Message-ID<adf4e759-2825-4ec8-9992-a0a6a91c0843@googlegroups.com>
In reply to#41767
Jiewei Huang於 2013年3月24日星期日UTC+1上午6時20分29秒寫道:
> Hi all,
> 
> 
> 
> Currently create a simple text-based database of information about people
> 
>  
> 
> 
> 
> I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
> 
> Name	 Address	Telephone	Birthday  
> 
> John Konon	Ministry of Moon Walks	4567882	27-Feb
> 
> Stacy Kisha	Ministry of Man Power	1234567	17-Jan
> 
> 
> 
> 
> 
> My codes are :
> 
> import csv
> 
> original = file('friends.csv', 'rU')
> 
> reader = csv.reader(original)
> 
> 
> 
> for row in reader:
> 
>     
> 
>     print row
> 
> 
> 
> 
> 
> and the output is :
> 
> ['Name', ' Address', 'Telephone', 'Birthday']
> 
> ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
> 
> ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']
> 
> 
> 
> But i wanted to make it
> 
> 
> 
> [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
> 
> ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
> 
> 
> 
> can someone show me guidance to this issue
> 
> 
> 
> Thanks all

import csv
original = file('friends.csv', 'rU')
reader = csv.reader(original)
print [tuple(row) for row in reader][1:]

is this you want?
Note that:
    1) I assume your csv file content are seperated by comma
    2) this way allocates your memory, will be a trouble when a big table :P

[toc] | [prev] | [next] | [standalone]


#41815

FromJiewei Huang <jiewei24@gmail.com>
Date2013-03-24 23:52 -0700
Message-ID<4e1e93e5-14bd-4408-9c92-3c340e558deb@googlegroups.com>
In reply to#41777
On Sunday, March 24, 2013 9:10:45 PM UTC+10, ypsun wrote:
> Jiewei Huang於 2013年3月24日星期日UTC+1上午6時20分29秒寫道:
> 
> > Hi all,
> 
> > 
> 
> > 
> 
> > 
> 
> > Currently create a simple text-based database of information about people
> 
> > 
> 
> >  
> 
> > 
> 
> > 
> 
> > 
> 
> > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
> 
> > 
> 
> > Name	 Address	Telephone	Birthday  
> 
> > 
> 
> > John Konon	Ministry of Moon Walks	4567882	27-Feb
> 
> > 
> 
> > Stacy Kisha	Ministry of Man Power	1234567	17-Jan
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > My codes are :
> 
> > 
> 
> > import csv
> 
> > 
> 
> > original = file('friends.csv', 'rU')
> 
> > 
> 
> > reader = csv.reader(original)
> 
> > 
> 
> > 
> 
> > 
> 
> > for row in reader:
> 
> > 
> 
> >     
> 
> > 
> 
> >     print row
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > 
> 
> > and the output is :
> 
> > 
> 
> > ['Name', ' Address', 'Telephone', 'Birthday']
> 
> > 
> 
> > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
> 
> > 
> 
> > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']
> 
> > 
> 
> > 
> 
> > 
> 
> > But i wanted to make it
> 
> > 
> 
> > 
> 
> > 
> 
> > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
> 
> > 
> 
> > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
> 
> > 
> 
> > 
> 
> > 
> 
> > can someone show me guidance to this issue
> 
> > 
> 
> > 
> 
> > 
> 
> > Thanks all
> 
> 
> 
> import csv
> 
> original = file('friends.csv', 'rU')
> 
> reader = csv.reader(original)
> 
> print [tuple(row) for row in reader][1:]
> 
> 
> 
> is this you want?
> 
> Note that:
> 
>     1) I assume your csv file content are seperated by comma
> 
>     2) this way allocates your memory, will be a trouble when a big table :P

Hi guys, i got into another situation i was told not to use csv library and currently my code is :

f = open('friends.csv', 'rU')
for row in f:
        
        print [row for (row) in f]

output :
['John Cleese,Ministry of Silly Walks,5555421,27-Oct\n', 'Stacy Kisha,Ministry of Man Power,1234567,17-Jan\n']

i need it to become :
[('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Oct'), ('Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan')]

guide me please

[toc] | [prev] | [next] | [standalone]


#41825

Fromrusi <rustompmody@gmail.com>
Date2013-03-25 06:51 -0700
Message-ID<f085bd7b-2bb3-46be-88b5-292e5f181a95@kw7g2000pbb.googlegroups.com>
In reply to#41815
On Mar 25, 11:52 am, Jiewei Huang <jiewe...@gmail.com> wrote:
> On Sunday, March 24, 2013 9:10:45 PM UTC+10, ypsun wrote:
> > Jiewei Huang於 2013年3月24日星期日UTC+1上午6時20分29秒寫道:
>
> > > Hi all,
>
> > > Currently create a simple text-based database of information about people
>
> > > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
>
> > > Name   Address        Telephone       Birthday
>
> > > John Konon    Ministry of Moon Walks  4567882 27-Feb
>
> > > Stacy Kisha   Ministry of Man Power   1234567 17-Jan
>
> > > My codes are :
>
> > > import csv
>
> > > original = file('friends.csv', 'rU')
>
> > > reader = csv.reader(original)
>
> > > for row in reader:
>
> > >     print row
>
> > > and the output is :
>
> > > ['Name', ' Address', 'Telephone', 'Birthday']
>
> > > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
>
> > > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']
>
> > > But i wanted to make it
>
> > > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
>
> > > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
>
> > > can someone show me guidance to this issue
>
> > > Thanks all
>
> > import csv
>
> > original = file('friends.csv', 'rU')
>
> > reader = csv.reader(original)
>
> > print [tuple(row) for row in reader][1:]
>
> > is this you want?
>
> > Note that:
>
> >     1) I assume your csv file content are seperated by comma
>
> >     2) this way allocates your memory, will be a trouble when a big table :P
>
> Hi guys, i got into another situation i was told not to use csv library and currently my code is :
>
> f = open('friends.csv', 'rU')
> for row in f:
>
>         print [row for (row) in f]
>
> output :
> ['John Cleese,Ministry of Silly Walks,5555421,27-Oct\n', 'Stacy Kisha,Ministry of Man Power,1234567,17-Jan\n']
>
> i need it to become :
> [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Oct'), ('Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan')]
>
> guide me please

Have you tried the split (and perhaps strip) methods from
http://docs.python.org/2/library/stdtypes.html#string-methods
?

[toc] | [prev] | [next] | [standalone]


#41844

FromJiewei Huang <jiewei24@gmail.com>
Date2013-03-25 18:05 -0700
Message-ID<3054bd15-f3d3-4568-aea2-567dd6d762de@googlegroups.com>
In reply to#41825
On Monday, March 25, 2013 11:51:51 PM UTC+10, rusi wrote:
> On Mar 25, 11:52 am, Jiewei Huang <jiewe...@gmail.com> wrote:
> 
> > On Sunday, March 24, 2013 9:10:45 PM UTC+10, ypsun wrote:
> 
> > > Jiewei Huang於 2013年3月24日星期日UTC+1上午6時20分29秒寫道:
> 
> >
> 
> > > > Hi all,
> 
> >
> 
> > > > Currently create a simple text-based database of information about people
> 
> >
> 
> > > > I have a csv file which consist of 3 rows , row 1 2 and 3 is as such:
> 
> >
> 
> > > > Name   Address        Telephone       Birthday
> 
> >
> 
> > > > John Konon    Ministry of Moon Walks  4567882 27-Feb
> 
> >
> 
> > > > Stacy Kisha   Ministry of Man Power   1234567 17-Jan
> 
> >
> 
> > > > My codes are :
> 
> >
> 
> > > > import csv
> 
> >
> 
> > > > original = file('friends.csv', 'rU')
> 
> >
> 
> > > > reader = csv.reader(original)
> 
> >
> 
> > > > for row in reader:
> 
> >
> 
> > > >     print row
> 
> >
> 
> > > > and the output is :
> 
> >
> 
> > > > ['Name', ' Address', 'Telephone', 'Birthday']
> 
> >
> 
> > > > ['John Konon', 'Ministry of Moon Walks', '4567882', '27-Feb']
> 
> >
> 
> > > > ['Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan']
> 
> >
> 
> > > > But i wanted to make it
> 
> >
> 
> > > > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Feb'),
> 
> >
> 
> > > > ( 'Stacy Kisha', 'Ministry of Man Power', '1234567', 17-Jan')]
> 
> >
> 
> > > > can someone show me guidance to this issue
> 
> >
> 
> > > > Thanks all
> 
> >
> 
> > > import csv
> 
> >
> 
> > > original = file('friends.csv', 'rU')
> 
> >
> 
> > > reader = csv.reader(original)
> 
> >
> 
> > > print [tuple(row) for row in reader][1:]
> 
> >
> 
> > > is this you want?
> 
> >
> 
> > > Note that:
> 
> >
> 
> > >     1) I assume your csv file content are seperated by comma
> 
> >
> 
> > >     2) this way allocates your memory, will be a trouble when a big table :P
> 
> >
> 
> > Hi guys, i got into another situation i was told not to use csv library and currently my code is :
> 
> >
> 
> > f = open('friends.csv', 'rU')
> 
> > for row in f:
> 
> >
> 
> >         print [row for (row) in f]
> 
> >
> 
> > output :
> 
> > ['John Cleese,Ministry of Silly Walks,5555421,27-Oct\n', 'Stacy Kisha,Ministry of Man Power,1234567,17-Jan\n']
> 
> >
> 
> > i need it to become :
> 
> > [('John Cleese', 'Ministry of Silly Walks', '5555421', '27-Oct'), ('Stacy Kisha', 'Ministry of Man Power', '1234567', '17-Jan')]
> 
> >
> 
> > guide me please
> 
> 
> 
> Have you tried the split (and perhaps strip) methods from
> 
> http://docs.python.org/2/library/stdtypes.html#string-methods
> 
> ?
can show me one line of how to implement it base on my problem?

[toc] | [prev] | [next] | [standalone]


#41845

FromDave Angel <davea@davea.name>
Date2013-03-25 21:40 -0400
Message-ID<mailman.3710.1364262083.2939.python-list@python.org>
In reply to#41844
On 03/25/2013 09:05 PM, Jiewei Huang wrote:
 > On Monday, March 25, 2013 11:51:51 PM UTC+10, rusi wrote:

If you insist on using GoogleGroups, then make sure you keep your quotes 
small.  I'm about to stop reading messages that are double-spaced by 
buggy software.

>>> <SNIP>
>>

>>
>>
>> Have you tried the split (and perhaps strip) methods from
>>
>> http://docs.python.org/2/library/stdtypes.html#string-methods
>>
>> ?

You got lots of specific advice from your previous thread.  So which 
version did you end up using?  It'd make a good starting place for this 
"problem."



> can show me one line of how to implement it base on my problem?
>

As long as the input data is constrained not to have any embedded 
commas, just use:

     mylist = line.split(",")


instead of print, send your output to a list.  Then for each line in the 
list, fix the bracket problem to your strange specs.

     outline = outline.replace("[", "(")

-- 
-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#41847

FromJiewei Huang <jiewei24@gmail.com>
Date2013-03-25 20:33 -0700
Message-ID<df59e5f7-8e00-4048-8878-a62e08960524@googlegroups.com>
In reply to#41845
On Tuesday, March 26, 2013 11:40:51 AM UTC+10, Dave Angel wrote:
> On 03/25/2013 09:05 PM, Jiewei Huang wrote:
> 
>  > On Monday, March 25, 2013 11:51:51 PM UTC+10, rusi wrote:
> 
> 
> 
> If you insist on using GoogleGroups, then make sure you keep your quotes 
> 
> small.  I'm about to stop reading messages that are double-spaced by 
> 
> buggy software.
> 
> 
> 
> >>> <SNIP>
> 
> >>
> 
> 
> 
> >>
> 
> >>
> 
> >> Have you tried the split (and perhaps strip) methods from
> 
> >>
> 
> >> http://docs.python.org/2/library/stdtypes.html#string-methods
> 
> >>
> 
> >> ?
> 
> 
> 
> You got lots of specific advice from your previous thread.  So which 
> 
> version did you end up using?  It'd make a good starting place for this 
> 
> "problem."
> 
> 
> 
> 
> 
> 
> 
> > can show me one line of how to implement it base on my problem?
> 
> >
> 
> 
> 
> As long as the input data is constrained not to have any embedded 
> 
> commas, just use:
> 
> 
> 
>      mylist = line.split(",")
> 
> 
> 
> 
> 
> instead of print, send your output to a list.  Then for each line in the 
> 
> list, fix the bracket problem to your strange specs.
> 
> 
> 
>      outline = outline.replace("[", "(")
> 
> 
> 
> -- 
> 
> -- 
> 
> DaveA

Hi Dave thanks for the tips,

I manage to code this:
f = open('Book1.csv', 'rU')
for row in f:
    print zip([row for (row) in f])

however my output is 
[('John Konon Ministry of Moon Walks 4567882 27-Feb\n',), ('Stacy Kisha Ministry of Man Power 1234567 17-Jan\n',)]

is there any method to remove the \n ?

[toc] | [prev] | [next] | [standalone]


#41850

FromMRAB <python@mrabarnett.plus.com>
Date2013-03-26 03:48 +0000
Message-ID<mailman.3713.1364269874.2939.python-list@python.org>
In reply to#41847
On 26/03/2013 03:33, Jiewei Huang wrote:
> On Tuesday, March 26, 2013 11:40:51 AM UTC+10, Dave Angel wrote:
>> On 03/25/2013 09:05 PM, Jiewei Huang wrote:
>>> On Monday, March 25, 2013 11:51:51 PM UTC+10, rusi wrote:
>>
>> If you insist on using GoogleGroups, then make sure you keep your quotes
>> small.  I'm about to stop reading messages that are double-spaced by
>> buggy software.
>>
>> >>> <SNIP>
>> >> Have you tried the split (and perhaps strip) methods from
>> >>
>> >> http://docs.python.org/2/library/stdtypes.html#string-methods
>> >>
>> >> ?
>>
>> You got lots of specific advice from your previous thread.  So which
>> version did you end up using?  It'd make a good starting place for this
>> "problem."
>>
>> > can show me one line of how to implement it base on my problem?
>> >
>>
>> As long as the input data is constrained not to have any embedded
>> commas, just use:
>>
>>      mylist = line.split(",")
>>
>> instead of print, send your output to a list.  Then for each line in the
>> list, fix the bracket problem to your strange specs.
>>
>>      outline = outline.replace("[", "(")
>
> Hi Dave thanks for the tips,
>
> I manage to code this:
> f = open('Book1.csv', 'rU')
> for row in f:
>      print zip([row for (row) in f])
>
> however my output is
> [('John Konon Ministry of Moon Walks 4567882 27-Feb\n',), ('Stacy Kisha Ministry of Man Power 1234567 17-Jan\n',)]
>
> is there any method to remove the \n ?
>
Use the .rstrip method:

     print zip(row.rstrip('\n') for row in f)

[toc] | [prev] | [next] | [standalone]


#41862

FromJiewei Huang <jiewei24@gmail.com>
Date2013-03-26 00:24 -0700
Message-ID<749d7e2c-7a31-451e-8dbb-0594a52e8b91@googlegroups.com>
In reply to#41850
On Tuesday, March 26, 2013 1:48:10 PM UTC+10, MRAB wrote:
> On 26/03/2013 03:33, Jiewei Huang wrote:
> 
> > On Tuesday, March 26, 2013 11:40:51 AM UTC+10, Dave Angel wrote:
> 
> >> On 03/25/2013 09:05 PM, Jiewei Huang wrote:
> 
> >>> On Monday, March 25, 2013 11:51:51 PM UTC+10, rusi wrote:
> 
> >>
> 
> >> If you insist on using GoogleGroups, then make sure you keep your quotes
> 
> >> small.  I'm about to stop reading messages that are double-spaced by
> 
> >> buggy software.
> 
> >>
> 
> >> >>> <SNIP>
> 
> >> >> Have you tried the split (and perhaps strip) methods from
> 
> >> >>
> 
> >> >> http://docs.python.org/2/library/stdtypes.html#string-methods
> 
> >> >>
> 
> >> >> ?
> 
> >>
> 
> >> You got lots of specific advice from your previous thread.  So which
> 
> >> version did you end up using?  It'd make a good starting place for this
> 
> >> "problem."
> 
> >>
> 
> >> > can show me one line of how to implement it base on my problem?
> 
> >> >
> 
> >>
> 
> >> As long as the input data is constrained not to have any embedded
> 
> >> commas, just use:
> 
> >>
> 
> >>      mylist = line.split(",")
> 
> >>
> 
> >> instead of print, send your output to a list.  Then for each line in the
> 
> >> list, fix the bracket problem to your strange specs.
> 
> >>
> 
> >>      outline = outline.replace("[", "(")
> 
> >
> 
> > Hi Dave thanks for the tips,
> 
> >
> 
> > I manage to code this:
> 
> > f = open('Book1.csv', 'rU')
> 
> > for row in f:
> 
> >      print zip([row for (row) in f])
> 
> >
> 
> > however my output is
> 
> > [('John Konon Ministry of Moon Walks 4567882 27-Feb\n',), ('Stacy Kisha Ministry of Man Power 1234567 17-Jan\n',)]
> 
> >
> 
> > is there any method to remove the \n ?
> 
> >
> 
> Use the .rstrip method:
> 
> 
> 
>      print zip(row.rstrip('\n') for row in f)

thanks ! got it working!

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web