reading dictionary's (key,value) from file

A

ankitks.mital

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
 
D

Diez B. Roggisch

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
 
R

Robert Bossy

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
 
A

ankitks.mital

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
 
T

Terry Reedy

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

Raymond Hettinger

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top