[Tutor] CSV module?

Liam Clarke cyresse at gmail.com
Sun Nov 21 03:17:21 CET 2004


Kent, 

As always, you are a beam of light upon the darkness of my illogicity
and/or ignorance.

You're right, I wasn't reopening the inputfile after doing
readlines(), which I was doing each time I opened the file! So, I
guess there's a pointer for each file object, that readline
increments.

If I wanted to reset that pointer for fileObject=file('erm.scc', 'r'),
could I just use

fileObject.seek(0)?


Thanks once again,

Liam Clarke

On Sat, 20 Nov 2004 20:40:13 -0500, Kent Johnson <kent37 at tds.net> wrote:
> Liam,
> 
> I can't be sure without seeing your whole program, but my guess is you
> either
> - didn't close the output file before opening for read, or
> - didn't re-open the input file between trying readlines() and reading
> with the csv reader, so the csv reader was just getting the end-of-file.
> 
> Anyway, here is a version of your snippets that works:
> 
> import csv
> 
> spreadS=[['A','B','C'],[1,2,3],[4,5,6]]
> dave=file('test.csv', 'w')
> henry=csv.writer(dave)
> henry.writerows(spreadS)
> dave.close()
> 
> x=file('test.csv','r')
> bob=csv.reader(x)
> for element in bob:
>       print element
> 
> 
> Kent
> 
> 
> 
> 
> Liam Clarke wrote:
> > Hi all,
> >
> > Trying to work the CSV module, and I'm not quite sure how to
> > input/output through it.
> >
> > I've worked out to output a CSV file that looks like -
> >
> > A B C
> > 1  2 3
> > 4  5 6
> >
> > you have to use a list like this -
> > spreadS=[['A','B','C'],[1,2,3],[4,5,6]], so each row is a separate
> > list.
> >
> > and then
> >
> > dave=file('test.csv', 'w')
> > henry=csv.writer(dave)
> >
> > and either
> >
> > for element in dave:
> >     henry.writerow(element)
> >
> > or just
> >
> > henry.writerows(dave).
> >
> > So writing is fine. But, if I were to reopen test.csv -
> >
> > x=file('test.csv','r')
> >
> > I can do this -
> >
> >
> >>>>print x.readlines()
> >
> > ['A,B,C\r\n', '1,2,3\r\n', '4,5,6\r\n']
> >
> > which is roughly what I expected.
> >
> > Howeever, if I were to do -
> >
> >
> >>>>bob=csv.reader(x)
> >
> >
> > And as per the Python docs -
> >
> > for element in bob:
> >      print element
> >
> > I get nothing.
> >
> > If I try bob.next(), I get 'StopIteration' which means that it's
> > reached the end of the file.
> >
> > So help? I'm probably doing something quite simple & silly, but the
> > documentation isn't much help (to me), and I've got not spreadsheet
> > software installed on this comp to check that my csv's are valid.
> >
> > I figure if I can pipe some data out through the CSV module and then
> > pipe it back in and nothing's changed then I'm doing OK.
> >
> > Regards,
> >
> > Liam Clarke
> > 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list