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?
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')]