Hi Ben,
Hi,
I want to get access to my abook address file with python.
Does anyone have some python lines to achive this using
curses? If not, maybe anybody has small python program doing
it with a gui!?
You can just parse the abook addressbook with the ConfigParser, try
this:
import os
from ConfigParser import *
abook = ConfigParser()
abook.read(os.environ["HOME"] + "/.abook/addressbook")
for s in abook.sections():
print abook.items(s)
Thanks! I found a different example too:
import ConfigParser
import string
config = ConfigParser.ConfigParser()
config.read("/home/fab/.abook/addressbook")
# print summary
print
for number in [2,200]:
print string.upper(config.get(str(number), "email"))
print string.upper(config.get(str(number), "name"))
print string.upper(config.get(str(number), "city"))
print string.upper(config.get(str(number), "address"))
but the problem seems to be that abook does not write every
field, so I get an exception when there is a field missing:
Traceback (most recent call last):
File "configparser-example-1.py", line 13, in ?
print string.upper(config.get(str(number), "city"))
File "/usr/lib/python2.4/ConfigParser.py", line 520, in get
raise NoOptionError(option, section)
ConfigParser.NoOptionError: No option 'city' in section: '2'
Section 2 looks like:
[2]
name=Andrs Gzi
[email protected]
nick=oz
Is there a workaround?
You can construct the parser with a dictionary of defaults:
config = ConfigParser.ConfigParser({"city" : "unknown",
"zip" : "unknown"})
that kind of thing.
Or catch the exceptions. Or use config.options("2") to see what options
exist in section 2 before you try to read them.