Separate Rows in reader

Jiewei Huang jiewei24 at gmail.com
Tue Mar 26 02:05:12 CET 2013


On Monday, March 25, 2013 11:51:51 PM UTC+10, rusi wrote:
> On Mar 25, 11:52 am, Jiewei Huang <jiewe... at 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?



More information about the Python-list mailing list