Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Separate Rows in reader

Reply
Thread Tools

Separate Rows in reader

 
 
Jiewei Huang
Guest
Posts: n/a
 
      03-24-2013
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
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      03-24-2013
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
 
Reply With Quote
 
 
 
 
rusi
Guest
Posts: n/a
 
      03-24-2013
On Mar 24, 10:46*am, Dave Angel <(E-Mail Removed)> 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 ','
 
Reply With Quote
 
Jiewei Huang
Guest
Posts: n/a
 
      03-24-2013
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']
 
Reply With Quote
 
Jiewei Huang
Guest
Posts: n/a
 
      03-24-2013
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']
 
Reply With Quote
 
Jiewei Huang
Guest
Posts: n/a
 
      03-24-2013
On Sunday, March 24, 2013 5:34:03 PM UTC+10, rusi wrote:
> On Mar 24, 10:46*am, Dave Angel <(E-Mail Removed)> 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 ','

 
Reply With Quote
 
ypsun
Guest
Posts: n/a
 
      03-24-2013
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
Note that I assume your csv file content are comma seperated.
 
Reply With Quote
 
ypsun
Guest
Posts: n/a
 
      03-24-2013
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
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      03-24-2013
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
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      03-24-2013
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Separate Tabs, Separate Sessions BigAndy Firefox 0 05-09-2007 09:27 AM
Separate Tabs, Separate Sessions BigAndy Firefox 0 05-09-2007 09:26 AM
Using separate classpaths for separate classes? Frank Fredstone Java 1 06-27-2006 06:46 AM
script for moving rows up and down and traverse thru rows of HTML table Subba Rao via DotNetMonster.com ASP .Net 0 03-19-2005 06:46 AM
How to use several separate classes (separate files) to be executed in one class (another file) EvgueniB Java 1 12-15-2003 01:18 AM



Advertisments