reading raw variables from file

Discussion in 'Python' started by Astan Chee, Nov 30, 2007.

  1. Astan Chee

    Astan Chee Guest

    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
     
    Astan Chee, Nov 30, 2007
    #1
    1. Advertising

  2. Astan Chee

    Paddy Guest

    On Nov 30, 4:57 am, Astan Chee <> wrote:
    > 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.
     
    Paddy, Nov 30, 2007
    #2
    1. Advertising

  3. Astan Chee

    Martin Blume Guest

    "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
     
    Martin Blume, Nov 30, 2007
    #3
  4. 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
     
    Bruno Desthuilliers, Nov 30, 2007
    #4
  5. Astan Chee

    Martin Blume Guest

    "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
     
    Martin Blume, Nov 30, 2007
    #5
  6. Astan Chee

    MonkeeSage Guest

    On Nov 30, 10:05 am, "Martin Blume" <> wrote:
    > "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
     
    MonkeeSage, Dec 1, 2007
    #6
  7. Astan Chee

    Martin Blume Guest

    "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
     
    Martin Blume, Dec 2, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. christos panagiotou

    reading .raw files (windows-linux)

    christos panagiotou, Jul 16, 2003, in forum: C++
    Replies:
    2
    Views:
    1,023
    Vasileios Zografos
    Jul 16, 2003
  2. Joan
    Replies:
    1
    Views:
    544
    Jonathan Mcdougall
    Nov 24, 2004
  3. Ivo Woltring

    Reading raw data from disc

    Ivo Woltring, Dec 13, 2004, in forum: Python
    Replies:
    1
    Views:
    407
    Grant Edwards
    Dec 13, 2004
  4. Gnurto
    Replies:
    3
    Views:
    1,281
    Victor Bazarov
    Dec 5, 2005
  5. Replies:
    8
    Views:
    412
Loading...

Share This Page