Separate Rows in reader

J

Jiewei Huang

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
 
D

Dave Angel

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.
 
R

rusi

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.

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

Jiewei Huang

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.


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

Jiewei Huang

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.


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

Jiewei Huang

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
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 ','
 
Y

ypsun

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.
 
Y

ypsun

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
 
D

Dave Angel

<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 "-----------"
 
T

Tim Chase

[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
 
R

rusi

[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:


Stripping of the first line is:

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

Or else

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:
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).
 
T

Tim Chase

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:

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
 
J

Jiewei Huang

On Sunday, March 24, 2013 3:20:29 PM UTC+10, Jiewei Huang wrote:

thanks to you guys i got my desire outcome! i will continue to code on writing and saving of csv file
 
R

rusi

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!
 
J

Jiewei Huang

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 andcurrently 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
 
R

rusi

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 whena 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'), ('StacyKisha', '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
?
 
J

Jiewei Huang

Jiewei Huangæ–¼ 2013å¹´3月24日星期日UTC+1上åˆ6時20分29秒寫é“:
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 libraryand 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?
 
D

Dave Angel

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.

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("[", "(")
 
J

Jiewei Huang

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.





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 ?
 
J

Jiewei Huang

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.





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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top