File processing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gopal

    #1

    File processing

    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.

  • wittempj@hotmail.com

    #2
    Re: File processing

    The ConfigParser class is designed for this task, see


    Comment

    • Jeremy Jones

      #3
      Re: File processing

      Gopal wrote:
      [color=blue]
      >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.
      >
      >
      >[/color]
      Would this
      (http://www.python.org/doc/current/li...figParser.html) do what
      you need? It's part of the standard library.


      - JMJ

      Comment

      • Gopal

        #4
        Re: File processing

        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.

        Comment

        • Peter Hansen

          #5
          Re: File processing

          Gopal wrote:[color=blue]
          > 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.[/color]

          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

          Comment

          • Gopal

            #6
            Re: File processing

            Thank you very much. That works!!!

            Comment

            • Jeremy Jones

              #7
              Re: File processing

              Gopal wrote:
              [color=blue]
              >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.
              >
              >
              >[/color]
              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.Co nfigParser()

              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

              Comment

              • Bryan Olson

                #8
                Re: File processing

                Peter Hansen wrote:[color=blue]
                > Gopal wrote:[color=green]
                >> [...] 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.[/color][/color]
                [color=blue]
                > 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[/color]

                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

                Comment

                Working...