Adif parser in Python


I am one of many who is waiting for Raspberry Pi. For make waiting more fun I decided learn Python programming language, which will be one focused in Raspberry Pi Linux distribution. They claim it is ideal for beginners and easy to learn. Yes, it is, very friendly and intuitive. I like it!

I used it already for build some DXKeeper's tool. Next challenge/exercise was write adif parser, as I already did it in Perl, some year ago. This is always good for manipulating with logbooks.

Finally succeed, here is my piece of code, which read adif file into list of dictionaries and then is possible do anything with logbook data. Bottom's prints show: first qso data and number of loaded qso.

  1. import re
  3. file_name = 'ok4bx-main-20111231.ADI'
  5. def parse_adif(fn):
  6.     raw = re.split('<eor>|<eoh>(?i)',open(fn).read() )
  7.     raw.pop(0)  #remove header
  8.     raw.pop()   #remove last empty item
  9.     logbook =[]
  10.     for record in raw:
  11.          qso = {}
  12.          tags = re.findall('<(.*?):(\d+).*?>([^<\t\n\r\f\v\Z]+)',record)
  13.          for tag in tags:
  14.                 qso[tag[0].lower()] = tag[2][:int(tag[1])]
  15.          logbook.append(qso)    
  16.     return logbook
  18. l = parse_adif(file_name)
  19. print l[0]
  20. print len(l)


Works great - but one small

Works great - but one small challenge I found in the line 12:
^<\t\n\r\f\v\Z <- \Z
have to be deleted in Python3 - as it made several failures if I imported 49900 lines of ADIF :)
Removed it and it works perfect - 50k lines were imported in around 3 seconds!

73 Peter df 1 lx

Hi Peter, yes I found out

Hi Peter, yes I found out that as well. I have corrected it with export function here.
Glad it works for you :)

Thank you. It's a good

Thank you. It's a good starting point!

73 de CT1GVN