A problem with list

E

export

The following code
##############
import string
MyList=['abc','def']
for i in MyList:
print i
###############

works as I expect that is I get
abc
def

but when I have Mylist in a file and I read it from the file it does
not work as I expect.
#########
import string
ff=open('C:\\Robotp\\MyFile.txt','r') # read MyList from a file
MyList=ff.read()
for i in MyList:
print i
###########
I will get
[
'
a
b
c
'
,
'
d
e
f
'
]

where my MyFile.txt looks like this:
['abc','def']

Where is a problem?
Thanks for help
Lad
 
G

gawel

The following code
##############
import string
MyList=['abc','def']
for i in MyList:
print i
###############

works as I expect that is I get
abc
def

but when I have Mylist in a file and I read it from the file it does
not work as I expect.
#########
import string
ff=open('C:\\Robotp\\MyFile.txt','r') # read MyList from a file
MyList=ff.read()
for i in MyList:
print i
###########
I will get
[
'
a
b
c
'
,
'
d
e
f
'
]

where my MyFile.txt looks like this:
['abc','def']

Where is a problem?
Thanks for help
Lad

That's quite simple. Your file contain a string, not a list
You must use eval(your_list_as_string).

>>> f = open('/tmp/list')
>>> f.seek(0)
>>> s = f.read()
>>> s "['adc','def']\n"
>>> l = eval(s)
>>> l ['adc', 'def']
>>> for i in l:
.... print i
....
adc
def

gawel
 
R

Roy Smith

but when I have Mylist in a file and I read it from the file it does
not work as I expect.
#########
import string
ff=open('C:\\Robotp\\MyFile.txt','r') # read MyList from a file
MyList=ff.read()
for i in MyList:
print i
###########
I will get
[
'
a
b
c
'
,
'
d
e
f
'
]

where my MyFile.txt looks like this:
['abc','def']

The problem is that read() just reads in characters of text and stores
them in a string. It doesn't *execute* that text as if it were a
program. You want to do one of two things. The first, which requires
no changes to your file, would be to eval the text you read. For
example:
s = "['foo', 'bar']"
s "['foo', 'bar']"
type (s)
<type 'str'>

s is a string containing the printed representation of a list. You
turn that into a real list by passing the string to eval()
l = eval (s)
l ['foo', 'bar']
type (l)
<type 'list'>

Alternatively (and probably the direction you want to be looking), is
to alter your file a little and import it. Make your file look like:

x = ['abc', 'def']

Assuming the filename is "foo.py", you can do "import foo" and your
file will be read AND EXECUTED, and a module will be created with the
same name as the basename of the file. The variable x will be a
variable inside that module:
['abc', 'def']
 
J

Jeff Shannon

gawel said:
That's quite simple. Your file contain a string, not a list
You must use eval(your_list_as_string).


....except that eval() is a huge security hole. A better bet would be to
use either the listquote module mentioned elsewhere, or to find an
alternative way of storing your data that *doesn't* require using eval()
to read it back in from disk. One possibility would be the ConfigParser
module, which makes it easy to store (and retrieve) information from
..ini-style files and/or the Windows Registry.

Jeff Shannon
Technician/Programmer
Credit International
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,214
Messages
2,571,110
Members
47,703
Latest member
robert.marryson

Latest Threads

Top