Separate Rows in reader
Dave Angel
davea at davea.name
Sun Mar 24 09:03:31 EDT 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
More information about the Python-list
mailing list