By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

reading dictionary's (key,value) from file

P: n/a
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
Apr 7 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
an***********@gmail.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
Apr 7 '08 #2

P: n/a
an***********@gmail.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

Apr 7 '08 #3

P: n/a
an***********@gmail.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
Apr 7 '08 #4

P: n/a
On Apr 7, 11:55*am, Robert Bossy <Robert.Bo...@jouy.inra.frwrote:
ankitks.mi...@gmail.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
Apr 7 '08 #5

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

Apr 7 '08 #6

P: n/a
On Apr 7, 9:38*am, ankitks.mi...@gmail.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
Apr 8 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.