473,394 Members | 1,295 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,394 software developers and data experts.

reading raw variables from file

Hi,
I have a file that might contain literal python variable statements at
every line. For example the file info.dat looks like this:
users = ["Bob", "Jane"]
status = {1:"ok",2:users[0]}
the problem is I want to read this file and load whatever variables
written in it as normal python variable statements so that when i read
the file, my users var will be ["Bob","Jane"] and my status var will be
{1:"ok",2:users[0]} . Is there an easy way of doing this instead of
parsing the files and checking said types?
Thanks
Cheers
Astan
Nov 30 '07 #1
6 1489
On Nov 30, 4:57 am, Astan Chee <st...@al.com.auwrote:
Hi,
I have a file that might contain literal python variable statements at
every line. For example the file info.dat looks like this:
users = ["Bob", "Jane"]
status = {1:"ok",2:users[0]}
the problem is I want to read this file and load whatever variables
written in it as normal python variable statements so that when i read
the file, my users var will be ["Bob","Jane"] and my status var will be
{1:"ok",2:users[0]} . Is there an easy way of doing this instead of
parsing the files and checking said types?
Thanks
Cheers
Astan
Think SECURITY. If is someone likely to put malicious code in the file
to crap all over your machine?
If not then import the file as a module.

- Paddy.
Nov 30 '07 #2
"Astan Chee" schrieb
I have a file that might contain literal python
variable statements at every line. For example
the file info.dat looks like this:
users = ["Bob", "Jane"]
status = {1:"ok",2:users[0]}
the problem is I want to read this file and load
whatever variables written in it as normal python
variable statements so that when i read the file,
my users var will be ["Bob","Jane"] and my status
var will be {1:"ok",2:users[0]} .
Is there an easy way of doing this instead of
parsing the files and checking said types?
You might want to look at the eval, exec and execfile;
but bear in in mind Paddy's warning about security.

Regards
Martin
Nov 30 '07 #3
Martin Blume a écrit :
"Astan Chee" schrieb
>I have a file that might contain literal python
variable statements at every line. For example
the file info.dat looks like this:
users = ["Bob", "Jane"]
status = {1:"ok",2:users[0]}
the problem is I want to read this file and load
whatever variables written in it as normal python
variable statements so that when i read the file,
my users var will be ["Bob","Jane"] and my status
var will be {1:"ok",2:users[0]} .
Is there an easy way of doing this instead of
parsing the files and checking said types?

You might want to look at the eval, exec and execfile;
Or just import...
but bear in in mind Paddy's warning about security.
+10

Nov 30 '07 #4
"Bruno Desthuilliers" schrieb
I have a file that might contain literal python
variable statements at every line. For example
the file info.dat looks like this:
users = ["Bob", "Jane"]
status = {1:"ok",2:users[0]}
the problem is I want to read this file and load
whatever variables written in it as normal python
variable statements so that when i read the file,
my users var will be ["Bob","Jane"] and my status
var will be {1:"ok",2:users[0]} .
Is there an easy way of doing this instead of
parsing the files and checking said types?
You might want to look at the eval, exec and execfile;

Or just import...
but bear in in mind Paddy's warning about security.

+10
If I have understood python naming scoping correctly,
doing
my_var="hello"
import stuff
print my_var
is not the same as
my_var="hello"
exec open("stuff.py").read()
print my_var
with stuff.py containing
my_var="bye"

I use this exec open("stuff.py").read() mechanism to set
values in my scripts: the script sets a useful default,
a command-line argument in the form a valid python program
may override it. Why bother with inventing or using another
mechanism when this is perfectly simple, easy and self-explaining?

The one and only thing against it is that a malicious user
can sneak in an os.system("cd / && rm -rf *").

IMHO. YMMV.
Martin


Nov 30 '07 #5
On Nov 30, 10:05 am, "Martin Blume" <mbl...@socha.netwrote:
"Bruno Desthuilliers" schrieb
>I have a file that might contain literal python
>variable statements at every line. For example
>the file info.dat looks like this:
>users = ["Bob", "Jane"]
>status = {1:"ok",2:users[0]}
>the problem is I want to read this file and load
>whatever variables written in it as normal python
>variable statements so that when i read the file,
>my users var will be ["Bob","Jane"] and my status
>var will be {1:"ok",2:users[0]} .
>Is there an easy way of doing this instead of
>parsing the files and checking said types?
You might want to look at the eval, exec and execfile;
Or just import...
but bear in in mind Paddy's warning about security.
+10

If I have understood python naming scoping correctly,
doing
my_var="hello"
import stuff
print my_var
is not the same as
my_var="hello"
exec open("stuff.py").read()
print my_var
with stuff.py containing
my_var="bye"
It's not the same...

from stuff import *

....is.
I use this exec open("stuff.py").read() mechanism to set
values in my scripts: the script sets a useful default,
a command-line argument in the form a valid python program
may override it. Why bother with inventing or using another
mechanism when this is perfectly simple, easy and self-explaining?

The one and only thing against it is that a malicious user
can sneak in an os.system("cd / && rm -rf *").

IMHO. YMMV.
Martin
Regards,
Jordan
Dec 1 '07 #6
"MonkeeSage" schrieb

If I have understood python naming scoping correctly,
doing
my_var="hello"
import stuff
print my_var
is not the same as
my_var="hello"
exec open("stuff.py").read()
print my_var
with stuff.py containing
my_var="bye"

It's not the same...

from stuff import *

...is.
And indeed it is. Thanks.
Martin

Dec 2 '07 #7

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

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
7
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text file looks like this: --------
7
by: Daniel Moree | last post by:
I'm working on a program that must first establish if the file exists in the program directory then it must open if for reading, read each line and set the variables then the program goes on about...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
1
by: Andrea Gavana | last post by:
Hello NG, that may sound a silly question, but I didn't find anything really clear about the issue of reading unformatted big endian files with Python. What I was doing till now, was using...
4
waynetheengineer
by: waynetheengineer | last post by:
Hello everyone :) I am trying to write VB code for reading filenames and file property values in a specific directory. For example, I have a directory called C:/Bears and in that directory...
4
by: isaac2004 | last post by:
hello, what i am trying to do is set up a loop that grabs multiple variables from an input file. my question is, is there a built in function in the istream class that can grab multiple values on...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
5
by: imailz | last post by:
Hi all, since I'm forced to switch from Fortran to C I wonder if there is posibility in C: 1) to use implicit loops 2) to parse several variables which number is determined at runtime. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.