Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing and retrieving a list of Tuples using ConfigParser

I would like store some configuration data in a config file. Here's a sample section:

[URLs]
Google, www.google.com
Hotmail, www.hotmail.com
Yahoo, www.yahoo.com

Is it possible to read this into a list of tuples using the ConfigParser module? If not, what do I use?

like image 613
Mridang Agarwalla Avatar asked Dec 09 '22 13:12

Mridang Agarwalla


1 Answers

Can you change the separator from comma (,) to a semicolon (:) or use the equals (=) sign? In that case ConfigParser will automatically do it for you.

For e.g. I parsed your sample data after changing the comma to equals:

# urls.cfg
[URLs]
Google=www.google.com
Hotmail=www.hotmail.com
Yahoo=www.yahoo.com

# Scriptlet
import ConfigParser
filepath = '/home/me/urls.cfg'

config = ConfigParser.ConfigParser()
config.read(filepath)

print config.items('URLs') # Returns a list of tuples.
# [('hotmail', 'www.hotmail.com'), ('google', 'www.google.com'), ('yahoo', 'www.yahoo.com')]
like image 74
Manoj Govindan Avatar answered Jan 17 '23 17:01

Manoj Govindan