[Tutor] Reading List from File

S Python spython01 at gmail.com
Thu Jul 31 20:29:41 CEST 2008


Monika,

Thanks for your help.  I got it to work using the following (also had
to spell "delimiter"):

>>> import csv
>>> myfile = open(r'c:\test.txt', 'r')
>>> data  = csv.reader(myfile, delimiter=',')
>>> print data
<_csv.reader object at 0x00D41870>
>>> for item in data:
	print item

	
['aaa', 'bbb', 'ccc']

I think it was referred to in another post, but I have found this page
to be helpful:
http://docs.python.org/lib/csv-examples.html

Thanks.

Samir

On Thu, Jul 31, 2008 at 2:20 PM, Monika Jisswel
<monjissvel at googlemail.com> wrote:
> 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


More information about the Tutor mailing list