Re: [Tutor] Re ading List from File

2009-11-11 Thread Shashwat Anand



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.
>

Simply use split().
I appended these lines at the end of your piece of code,

>>> y = x[0].split(',')
>>> y
['"aaa"', '"bbb"', '"ccc"']
>>> len(y)
3
the reason for extra quotes is due to the fact that string is "aaa" and not
aaa and strings are encapsulated with "" and hence the extra quote.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Re ading List from File

2009-11-11 Thread furblender

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 
import csv
  File "H:/csv.py", line 2, in 
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@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.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor