Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2005, 07:41 PM
export@hope.cz
Guest
 
Posts: n/a
Default A problem with list

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




  #2  
Old July 18th, 2005, 07:41 PM
gawel
Guest
 
Posts: n/a
Default Re: A problem with list

export@hope.cz wrote:[color=blue]
> 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
>[/color]

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

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

gawel

  #3  
Old July 18th, 2005, 07:41 PM
Roy Smith
Guest
 
Posts: n/a
Default Re: A problem with list

<export@hope.cz> wrote:[color=blue]
>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'][/color]

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:
[color=blue][color=green][color=darkred]
>>> s = "['foo', 'bar']"
>>> s[/color][/color][/color]
"['foo', 'bar']"[color=blue][color=green][color=darkred]
>>> type (s)[/color][/color][/color]
<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()
[color=blue][color=green][color=darkred]
>>> l = eval (s)
>>> l[/color][/color][/color]
['foo', 'bar'][color=blue][color=green][color=darkred]
>>> type (l)[/color][/color][/color]
<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:
[color=blue][color=green][color=darkred]
>>> import foo
>>> foo.x[/color][/color][/color]
['abc', 'def']
  #4  
Old July 18th, 2005, 07:42 PM
Fuzzyman
Guest
 
Posts: n/a
Default Re: A problem with list

An alternative is reading the list into a string and using my
'listquote' module. It will tun strings into lists (and vice versa) -
including nested lists (lists of lists) and properly handling quoted
elements.

http://www.voidspace.org.uk/atlantib...thonutils.html

It won't do integer conversions though - only strings.

Regards,

Fuzzy

  #5  
Old July 18th, 2005, 07:42 PM
Jeff Shannon
Guest
 
Posts: n/a
Default Re: A problem with list

gawel wrote:
[color=blue]
> export@hope.cz wrote:
>[color=green]
>> but when I have Mylist in a file and I read it from the file it does
>> not work as I expect.[/color]
>
>
> That's quite simple. Your file contain a string, not a list
> You must use eval(your_list_as_string).[/color]


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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.