[Tutor] Reading List from File

Monika Jisswel monjissvel at googlemail.com
Thu Jul 31 20:20:28 CEST 2008


oops it is reader not Reader (all lower case), so this line : data =
csv.Reader(myfile, delimeter = ',')
should be data = csv.reader(myfile, delimeter = ',')


2008/7/31 S Python <spython01 at gmail.com>

> Hi Everyone,
>
> Thanks for the variety of responses in such a short amount of time.
> This distribution list is incredible.
>
> Sorry for the delayed reply as I wanted to test what everyone
> suggested, so here goes:
>
> -------------------------------
>
> @Amin:  I tried your suggestion, but perhaps I don't understand your
> syntax.  Here is what I tried and the resulting error message:
>
> >>> f = open(r'C:\test.txt', 'r')
> >>> foo = f.readline.split(',')
>
> Traceback (most recent call last):
>  File "<pyshell#16>", line 1, in <module>
>    foo = f.readline.split(',')
> AttributeError: 'builtin_function_or_method' object has no attribute
> 'split'
>
> Do you know what I did wrong?
>
> -------------------------------
>
> @Emad, Brett:  Thank you for your solutions.  They do exactly what I
> was looking for.
>
> -------------------------------
>
> @Chad:  Thanks for your suggestion.  I think I like it best for its
> simplicity.
>
> -------------------------------
>
> @Emile, Monika, kinuthi:  The CSV standard library looks interesting
> but I am having mixed results in implementing it.  For example, it
> works when I try this:
>
> >>> reader = csv.reader(open(r'c:\test.txt', 'rb'))
> >>> for row in reader:
>        print row
>
>
> ['aaa', 'bbb', 'ccc']
>
> but it fails when I try:
>
> >>> import csv
> >>> myfile = open(r'c:\test.txt', 'r')
> >>> data = csv.Reader(myfile, delimeter = ',')
>
> Traceback (most recent call last):
>  File "<pyshell#26>", line 1, in <module>
>     data = csv.Reader(myfile, delimeter = ',')
> AttributeError: 'module' object has no attribute 'Reader'
>
> The error looks similar to what I received when I tried Amin's
> approach.  Am I missing something?
>
> -------------------------------
>
> It's interesting to note that for the solutions to work correctly, I
> had to remove the quotation marks from the input file.
>
> Thanks again to EVERYONE who took the time to respond.  I appreciate your
> help.
>
> Samir
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080731/7cdcb26c/attachment.htm>


More information about the Tutor mailing list