reading dictionary's (key,value) from file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ankitks.mital@gmail.com

    reading dictionary's (key,value) from file

    Folks,
    Is it possible to read hash values from txt file.
    I have script which sets options. Hash table has key set to option,
    and values are option values.

    Way we have it, we set options in a different file (*.txt), and we
    read from that file.
    Is there easy way for just reading file and setting options instead of
    parsing it.

    so this is what my option files look like:

    1opt.txt
    { '-cc': '12',
    '-I': r'/my/path/work/'}

    2opt.txt
    { '-I': r/my/path/work2/'}

    so my scipt how has dictionary
    options = { '-cc' :'12'
    '-I': r'/my/path/work/:/my/path/work2/'}

    I am trying to avoid parsing
  • Diez B. Roggisch

    #2
    Re: reading dictionary's (key,value) from file

    ankitks.mital@g mail.com wrote:
    Folks,
    Is it possible to read hash values from txt file.
    I have script which sets options. Hash table has key set to option,
    and values are option values.
    >
    Way we have it, we set options in a different file (*.txt), and we
    read from that file.
    Is there easy way for just reading file and setting options instead of
    parsing it.
    >
    so this is what my option files look like:
    >
    1opt.txt
    { '-cc': '12',
    '-I': r'/my/path/work/'}
    >
    2opt.txt
    { '-I': r/my/path/work2/'}
    >
    so my scipt how has dictionary
    options = { '-cc' :'12'
    '-I': r'/my/path/work/:/my/path/work2/'}
    >
    I am trying to avoid parsing
    Use a well-known config-file-format such as the module ConfigParser
    supports.

    Diez

    Comment

    • Jeffrey Froman

      #3
      Re: reading dictionary's (key,value) from file

      ankitks.mital@g mail.com wrote:
      so this is what my option files look like:
      >
      1opt.txt
      { '-cc': '12',
      '-I': r'/my/path/work/'}
      You can turn these strings read from text files into actual dictionaries
      using eval:
      >>d = eval("{ '-cc': '12', '-I': r'/my/path/work/'}")
      >>d
      {'-I': '/my/path/work/', '-cc': '12'}
      >>type(d)
      <type 'dict'>

      Note that eval will happily execute all sorts of arbitrary code, so this is
      not a good solution if you don't fully trust your option file creators.

      It's also a bit clunky compared to simply importing. Since you already have
      dictionary-literal syntax in your text file, why not add a
      left-hand-operator and make it a module instead? For example:

      # opt1.py
      d = { '-cc': '12',
      '-I': r'/my/path/work/'}

      # main.py
      from opt1 import d


      --
      Jeffrey

      Comment

      • Robert Bossy

        #4
        Re: reading dictionary's (key,value) from file

        ankitks.mital@g mail.com wrote:
        Folks,
        Is it possible to read hash values from txt file.
        I have script which sets options. Hash table has key set to option,
        and values are option values.
        >
        Way we have it, we set options in a different file (*.txt), and we
        read from that file.
        Is there easy way for just reading file and setting options instead of
        parsing it.
        >
        so this is what my option files look like:
        >
        1opt.txt
        { '-cc': '12',
        '-I': r'/my/path/work/'}
        >
        2opt.txt
        { '-I': r/my/path/work2/'}
        >
        so my scipt how has dictionary
        options = { '-cc' :'12'
        '-I': r'/my/path/work/:/my/path/work2/'}
        >
        I am trying to avoid parsing
        >
        For this particular case, you can use the optparse module:
        Source code: Lib/optparse.py Choosing an argument parsing library: The standard library includes three argument parsing libraries: getopt: a module that closely mirrors the procedural C getopt API....


        Since you're obviously running commands with different set of options, I
        suggest you listen to Diez.

        Cheers,
        RB


        Comment

        • ankitks.mital@gmail.com

          #5
          Re: reading dictionary's (key,value) from file

          On Apr 7, 11:55 am, Robert Bossy <Robert.Bo...@j ouy.inra.frwrot e:
          ankitks.mi...@g mail.com wrote:
          Folks,
          Is it possible to read hash values from txt file.
          I have script which sets options. Hash table has key set to option,
          and values are option values.
          >
          Way we have it, we set options in a different file (*.txt), and we
          read from that file.
          Is there easy way for just reading file and setting options instead of
          parsing it.
          >
          so this is what my option files look like:
          >
          1opt.txt
          { '-cc': '12',
            '-I': r'/my/path/work/'}
          >
          2opt.txt
          {  '-I': r/my/path/work2/'}
          >
          so my scipt how has dictionary
          options = { '-cc' :'12'
                          '-I': r'/my/path/work/:/my/path/work2/'}
          >
          I am trying to avoid parsing
          >
          For this particular case, you can use the optparse module:http://docs.python.org/lib/module-optparse.html
          >
          Since you're obviously running commands with different set of options, I
          suggest you listen to Diez.
          >
          Cheers,
          RB- Hide quoted text -
          >
          - Show quoted text -
          I think I am missing lot regarding optparser module. My only option is
          to have option file, and I have couple of those..each user creates
          it's own. How can optparser help me in this regard

          Thank you

          Comment

          • Terry Reedy

            #6
            Re: reading dictionary's (key,value) from file

            You can use execfile (or exec, in 3.0) function to execute code in a file
            in the present context.



            Comment

            • Raymond Hettinger

              #7
              Re: reading dictionary's (key,value) from file

              On Apr 7, 9:38 am, ankitks.mi...@g mail.com wrote:
              Folks,
              Is it possible to read hash values from txt file.
              I have script which sets options. Hash table has key set to option,
              and values are option values.
              >
              Way we have it, we set options in a different file (*.txt), and we
              read from that file.
              Is there easy way for just reading file and setting options instead of
              parsing it.
              >
              so this is what my option files look like:
              >
              1opt.txt
              { '-cc': '12',
                '-I': r'/my/path/work/'}
              >
              2opt.txt
              {  '-I': r/my/path/work2/'}
              >
              so my scipt how has dictionary
              options = { '-cc' :'12'
                              '-I': r'/my/path/work/:/my/path/work2/'}
              >
              I am trying to avoid parsing
              With minimal changes, you can just import the files:

              opt1.py
              ------
              opts = { '-cc': '12',
              '-I': r'/my/path/work/'}


              opt2.py
              ------
              opts = { '-I': r/my/path/work2/'}


              main.py
              -------
              from opts1 import opts as opt1
              from opts2 import opts as opt2


              Raymond

              Comment

              Working...