File processing

Discussion in 'Python' started by Gopal, Sep 23, 2005.

  1. Gopal

    Gopal Guest

    Hello,

    I'm Gopal. I'm looking for a solution to the following problem:

    I need to create a text file config.txt having some parameters. I'm
    thinking of going with this format by having "Param Name - value". Note
    that the value is a string/number; something like this:

    PROJECT_ID = "E4208506"
    SW_VERSION = "18d"
    HW_VERSION = "2"

    In my script, I need to parse this config file and extract the Values
    of the parameters.

    I'm very new to python as you can understand from the problem. However,
    I've some project dealines. So I need your help in arriving at a simple
    and ready-made solution.

    Regards,
    Gopal.
    Gopal, Sep 23, 2005
    #1
    1. Advertising

  2. Gopal

    Guest

    , Sep 23, 2005
    #2
    1. Advertising

  3. Gopal

    Jeremy Jones Guest

    Gopal wrote:

    >Hello,
    >
    >I'm Gopal. I'm looking for a solution to the following problem:
    >
    >I need to create a text file config.txt having some parameters. I'm
    >thinking of going with this format by having "Param Name - value". Note
    >that the value is a string/number; something like this:
    >
    >PROJECT_ID = "E4208506"
    >SW_VERSION = "18d"
    >HW_VERSION = "2"
    >
    >In my script, I need to parse this config file and extract the Values
    >of the parameters.
    >
    >I'm very new to python as you can understand from the problem. However,
    >I've some project dealines. So I need your help in arriving at a simple
    >and ready-made solution.
    >
    >Regards,
    >Gopal.
    >
    >
    >

    Would this
    (http://www.python.org/doc/current/lib/module-ConfigParser.html) do what
    you need? It's part of the standard library.


    - JMJ
    Jeremy Jones, Sep 23, 2005
    #3
  4. Gopal

    Gopal Guest

    Thanks for the reference. However, I'm not understanding how to use it.
    Could you please provide with an example? Like I open the file, read
    line and give it to parser?

    Please help me.
    Gopal, Sep 23, 2005
    #4
  5. Gopal

    Peter Hansen Guest

    Gopal wrote:
    > Hello,
    >
    > I'm Gopal. I'm looking for a solution to the following problem:
    >
    > I need to create a text file config.txt having some parameters. I'm
    > thinking of going with this format by having "Param Name - value". Note
    > that the value is a string/number; something like this:
    >
    > PROJECT_ID = "E4208506"
    > SW_VERSION = "18d"
    > HW_VERSION = "2"
    >
    > In my script, I need to parse this config file and extract the Values
    > of the parameters.
    >
    > I'm very new to python as you can understand from the problem. However,
    > I've some project dealines. So I need your help in arriving at a simple
    > and ready-made solution.


    Luckily, you're already done! Just make sure the above file has a .py
    extension, say maybe "config.py", and then in code where you need to
    "parse" it and extract the values you can just do this:

    import config
    print 'Project Id is', config.PROJECT_ID

    -Peter
    Peter Hansen, Sep 23, 2005
    #5
  6. Gopal

    Gopal Guest

    Thank you very much. That works!!!
    Gopal, Sep 23, 2005
    #6
  7. Gopal

    Jeremy Jones Guest

    Gopal wrote:

    >Thanks for the reference. However, I'm not understanding how to use it.
    >Could you please provide with an example? Like I open the file, read
    >line and give it to parser?
    >
    >Please help me.
    >
    >
    >

    I had thought of recommending what Peter Hansen recommended - just
    importing the text you have as a Python module. I don't know why I
    recommended ConfigParser over that option. However, if you don't like
    what Peter said and would still like to look at ConfigParser, here is a
    very simple example. Here is the config file I created from your email:

    jmjones@qiwi 8:36AM configparser % cat foo.txt
    [main]
    PROJECT_ID = "E4208506"
    SW_VERSION = "18d"
    HW_VERSION = "2"


    Here is me running ConfigParser from a Python shell:

    In [1]: import ConfigParser

    In [2]: p = ConfigParser.ConfigParser()

    In [3]: p.read("foo.txt")
    Out[3]: ['foo.txt']

    In [4]: p.get("main", "PROJECT_ID")
    Out[4]: '"E4208506"'


    Note that the value of ("main", "PROJECT_ID") is a string which contains
    double quotes in it. If you take Peter's advice, you won't have that
    problem; the config file will preserve your types for you.

    HTH,

    - JMJ
    Jeremy Jones, Sep 23, 2005
    #7
  8. Gopal

    Bryan Olson Guest

    Peter Hansen wrote:
    > Gopal wrote:
    >> [...] I'm
    >> thinking of going with this format by having "Param Name - value". Note
    >> that the value is a string/number; something like this:
    >>
    >> PROJECT_ID = "E4208506"
    >> SW_VERSION = "18d"
    >> HW_VERSION = "2"
    >>
    >> In my script, I need to parse this config file and extract the Values
    >> of the parameters.


    > Luckily, you're already done! Just make sure the above file has a .py
    > extension, say maybe "config.py", and then in code where you need to
    > "parse" it and extract the values you can just do this:
    >
    > import config
    > print 'Project Id is', config.PROJECT_ID


    In many cases that's fine, but at least be aware that the party
    supplying the config data also gets the ability to change
    the behavior of the process in ways of his choosing.


    --
    --Bryan
    Bryan Olson, Sep 23, 2005
    #8
    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. Andy
    Replies:
    1
    Views:
    1,374
    J├╝rgen Exner
    Jan 20, 2004
  2. Maxim
    Replies:
    0
    Views:
    409
    Maxim
    Jul 7, 2003
  3. Long Le
    Replies:
    3
    Views:
    1,168
    Long Le
    Aug 11, 2004
  4. MWells
    Replies:
    2
    Views:
    413
    MWells
    Jan 11, 2005
  5. Hubert Hung-Hsien Chang
    Replies:
    2
    Views:
    451
    Michael Foord
    Sep 17, 2004
Loading...

Share This Page