Hi
There are many different ways to solve the problem that you are having.
The easiest if you are planning on only dealing with strings or a
predictable data structure would be to do something like this:
Code:
~~
#Pre: Pass in a str
Tim Henderson wrote:
def tokenizer(str, chr=' '): #heres a useful tool just like
StringTokenizer feature in Java
if chr != '': chr = chr[0]
else: chr = ' '
x = ""
tokens = [""]
z = 0
for x in str:
if x != chr:
tokens[z] = tokens[z] + x
else:
z
gawel wrote:
[EMAIL PROTECTED] wrote:
but when I have Mylist in a file and I read it from the file it does
not work as I expect.
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 b
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/atlantibots/pythonutils.html
It won't do integer conversio
[EMAIL PROTECTED] wrote:
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('
Title: RE: A problem with list
[EMAIL PROTECTED]
#- where my MyFile.txt looks like this:
#- ['abc','def']
The issue is that in the file you don't have a list, you have text, and there is the difference in the behaviour.
>>> l = ['abc',
<[EMAIL PROTECTED]> wrote:
>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
>'
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'