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

How to retrieve key value pairs in python

P: 3
Hi,
Iam new to python.

I have a config file as shown below in the same order .I need to retrieve key value pairs from config file and will use those values in my script


# Name and details
(
{ group => 'abc',
host => 'pqr.com',
user => 'anonymous',
src => '/var/tmp',
dest => '/tmp',
},
{group => 'abc',
host =>'pqr.com',
user => 'anonymous',
src => '/tmp'
dest => '/var/tmp'
},
{ group => 'pqr',
host =>'abc.com',
user => 'xyz',
src => '/home/pp',
dest => '/var/tmp',
},
{ group => 'xyz',
host =>'p.com',
user => 'x',
src => '/home/',
dest => '/tmp',
}
)

Each {
},is considerd as one block..Group,user,host are unique as well as repeated.

I have to read and parse the config file and display key and value pair.Pls help.

Key : group,Value : 'abc'(say)
key : host ,Value :'pqr.com'
Key : user, Value :'anonymous'
Key : src,Value :'/var/tmp',
key : dest,Value : '/tmp'


Thank you,
Apr 27 '12 #1

✓ answered by bvdet

Your data is misconfigured because of some missing commas. Assuming you add in the missing commas, a list of dictionaries could be compiled. You can use string method index, and string slicing in a while loop to compile the list. Enclose the index call in a try/except block, because index will raise a ValueError if no substring is found. Example where config is the full configuration string read from the file:
Expand|Select|Wrap|Line Numbers
  1. idx = 0
  2. dictList = []
  3. while True:
  4.     try:
  5.         start = config.index("{", idx)
  6.         end = config.index("}", start+1)
  7.         slice = config[start+1:end-1]
  8.         sliceList = [s.strip() for s in slice.split(",") if s.strip()]
  9.         dd = {}
  10.         for item in sliceList:
  11.             key, value = [s.strip() for s in item.split("=>")]
  12.             dd[key] = value
  13.         dictList.append(dd)
  14.         idx = end+1
  15.     except ValueError, e:
  16.         break
Would it not be better to use XML for your configuration data?

Share this Question
Share on Google+
4 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
Your data is misconfigured because of some missing commas. Assuming you add in the missing commas, a list of dictionaries could be compiled. You can use string method index, and string slicing in a while loop to compile the list. Enclose the index call in a try/except block, because index will raise a ValueError if no substring is found. Example where config is the full configuration string read from the file:
Expand|Select|Wrap|Line Numbers
  1. idx = 0
  2. dictList = []
  3. while True:
  4.     try:
  5.         start = config.index("{", idx)
  6.         end = config.index("}", start+1)
  7.         slice = config[start+1:end-1]
  8.         sliceList = [s.strip() for s in slice.split(",") if s.strip()]
  9.         dd = {}
  10.         for item in sliceList:
  11.             key, value = [s.strip() for s in item.split("=>")]
  12.             dd[key] = value
  13.         dictList.append(dd)
  14.         idx = end+1
  15.     except ValueError, e:
  16.         break
Would it not be better to use XML for your configuration data?
Apr 29 '12 #2

P: 3
Thanks for your respons.It helped me a lot.No i cant use any xml file..
I did in perl as below (sample code)
Expand|Select|Wrap|Line Numbers
  1. "eval /@temp" =@configfile
  2. for $job in @temp
  3.   for $jobdef in %$job
  4.     $jobdef -> {src} = '/tmp'
  5.     $jobdef -> {user} = x
Is there any way to do similar to this in python?

Thank you in advance
May 1 '12 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Since I don't know Perl, I can't help with "similar" code. I mentioned XML because your data would be ideal for it, and you can use an XML parser to get the data.
May 1 '12 #4

P: 3
Thanks for ur response again..Well,its a CSV file...I cant put the data in XML...Tried ur code,worked something well...Hw to print values associated with user,source,group,host,dest... pertaining to a single block {
}
...?
May 2 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.