[Tutor] Re ading List from File

furblender jamiebbbb at hotmail.com
Wed Nov 11 07:33:28 CET 2009


Hello everyone,

I to had the same problem and it pestered me to the nth degree. I had that
many problems I went to the python site and copied an example and used that
to test why it wasn't working -see below example and traceback report. I
wasted a lot of time trying to figure my issue out. Then it dawned on me - I
had called the main program csv.py. But even when I changed the name of the
file and ran the program I was still getting the same problem. Why? because
I had in the directory a file called csv.py which is exactly the file name
for the csv file that is used in the import function. I deleted all the
references of csv.py from the directory that I was executing the command
from. Magically it worked. I do feel stupid but I hope this helps others who
maybe experiencing the same issue. Check for erroneous version of files that
are called cvs.py. 

import csv
reader = csv.reader(open("test.csv", "rb"))
for row in reader:
    print row

Traceback (most recent call last):
  File "H:/csv.py", line 1, in <module>
    import csv
  File "H:/csv.py", line 2, in <module>
    reader=csv.reader(open(test.csv, "rb"))
AttributeError: 'module' object has no attribute 'reader'


Samir-16 wrote:
> 
> Hi Everyone,
> 
> I am trying to read a comma-delimitted list ("aaa","bbb","ccc") from a
> text
> file and assign those values to a list, x, such that:
> 
> x = ["aaa", "bbb", "ccc"]
> 
> The code that I have come up with looks like this:
> 
>>>> x = []
>>>> f = open(r'c:\test.txt', 'r')
>>>> x.extend(f.readlines())
>>>> x
> ['"aaa","bbb","ccc"']
> 
> If you look closely, there is an extra pair of single quotes (') that
> encapsulates the string.  Therefore, len(x) returns 1, instead of 3.  Is
> there a function to "separate" this list out?  I hope my question makes
> sense.
> 
> Thanks in advance.
> 
> Samir
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 

-- 
View this message in context: http://old.nabble.com/Reading-List-from-File-tp18754202p26296541.html
Sent from the Python - tutor mailing list archive at Nabble.com.



More information about the Tutor mailing list