Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

export contacts as cvs

   1  
   2  import sysinfo
   3  import e32
   4  import os
   5  import os.path
   6  import re
   7  import time
   8  import urllib
   9  import contacts
  10  import codecs
  11  
  12  CODEC='utf-16'
  13  CVSFILENAME='E:\\contactsdb.cvs'
  14  
  15  def getFieldtypenames():
  16      "Return the list of fields"
  17      dic = contacts.fieldtypemap
  18      num = [[v,k] for k,v in dic.items()]
  19      num.sort()
  20      if num[0][0]==0 and num[0][1]=='none':
  21          del num[0]
  22      return [v for k,v in num]
  23  
  24  
  25  def exportContacts(filename):
  26      messages = []
  27  
  28      f = None
  29      try:
  30          f = codecs.open(filename,'w+',CODEC)
  31      except:
  32          print 'error creation file'
  33  
  34      if not f:
  35          return -1
  36  
  37      fields = getFieldtypenames()
  38      fieldformat = u''.join(['%('+v+')s,' for v in fields ])[0:-1]
  39      fieldformat+= '\n'
  40      fieldname = u''.join([''+v+',' for v in fields ])[0:-1]
  41      f.write("%s\n"%fieldname)
  42      
  43      try:
  44          db = contacts.open()
  45          idlist = db.keys()
  46  
  47          for id in idlist:
  48              newdict = dict([[k,''] for k in fields])
  49              contact = db[id]
  50              for field in contact:
  51                  newdict[field.type]=field.value
  52              f.write(fieldformat%newdict)
  53      except:
  54          pass
  55      f.close()
  56          
  57  
  58  def main():
  59     exportContacts(CVSFILENAME)
  60  
  61  
  62  if __name__=='__main__':
  63      main()
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS