[Tutor] Reading List from File
S Python
spython01 at gmail.com
Thu Jul 31 20:03:55 CEST 2008
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
More information about the Tutor
mailing list