473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

Jul 18 '05 #1
4 1154
ex****@hope.cz 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('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

Jul 18 '05 #2
<ex****@hope.cz> 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
'
,
'
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:
import foo
foo.x

['abc', 'def']
Jul 18 '05 #3
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

Jul 18 '05 #4
gawel wrote:
ex****@hope.cz 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 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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Johnny | last post by:
Hi together, I'm new to XML and have problems with FOP (http://www.apache.org/dyn/closer.cgi/xml/fop) When I try to convert my .fo document to .pdf, I get this message: fo:39:26...
4
by: jhonyxxx | last post by:
I have the next programa in C++: #include <iostream.h> // C++ I/O routines #include <list.h> // The STL list class #include<stdio.h> #include <string.h> typedef struct { char nombre; int...
2
by: dinks | last post by:
Hi, I'm new to C++ and have been assigned a task which i dont completely understand. Any help would be greately appreciated. Here is the problem: The class "linkedListType" use the "assert"...
4
by: Kain0o0 | last post by:
Hello , thanks for any help in advance. Im writing some code for a c++ class and the proffessor wants us to implement a list template using arrays. I thought this would be an easy task but have...
1
by: darkstorm | last post by:
Please check this program...When I compiles it in VC.net, it gives the following error: =============== Common\Lib\PList.h(115): error C2440: '=' : cannot convert from 'ListNode *' to...
0
by: Tony Johansson | last post by:
Hello! I have two classes called Handle which is a template class and a class Integer which is not a template class. The Integer class is just a wrapper class for a primitive int with some...
7
by: Christian Christmann | last post by:
Hi, in the past I always appreciated your help and hope that you also can help me this time. I've spent many many hours but still can't solve the problem by myself and you are my last hope. ...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
7
by: spidey12345 | last post by:
can anybody help me with this i have a baddata.txt file that has certain data like this: " blah blah ere werew ss a s ef df ww erew asf" and i...
13
by: B. Williams | last post by:
I have written some code to accept input and place this input at the beginning or end of a list, but I need assistance including a class so that it will allow input of a phone number that is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.