mcottondesign

Loving Open-Souce One Anonymous Function at a Time.

Got a problem, python is usually the answer

We had an emergency where our EMR provider isn't output the needed government reports. This wouldn't be such a big deal if they weren't due on Monday.

The problem seems to be with the XML records and some information needed to be striped out and manually looked at.

Thankfully python has easy installers for Windows and the standard lib includes xml parsing and csv output.

##########################################
###   Get info from xml and make a csv ###
##########################################

# Simple use:
# pullInfo('C:\\Users\\mcotton\\Desktop\\xml\\431530.xml')

# loop through all xml in directory
# by default it runs in current directory and
# creates a file named out.csv

import os, glob
from xml.dom.minidom import parseString, parse
import csv

def pullInfo(fileName):
  """ Gets information from Aprima xml file"""

  datasource = open(fileName)

  dom = parse(datasource)

  firstName = dom.getElementsByTagName('given')[0].toxml()
  firstName = firstName.replace('<given>','').replace('</given>','')

  lastName = dom.getElementsByTagName('family')[0].toxml()
  lastName = lastName.replace('<family>','').replace('</family>','')

  street = dom.getElementsByTagName('streetAddressLine')[0].toxml()  
  street =     street.replace('<streetAddressLine>','').replace('</streetAddressLine>','')

  city = dom.getElementsByTagName('city')[0].toxml()
  city = city.replace('<city>','').replace('</city>','')

  state = dom.getElementsByTagName('state')[0].toxml()
  state = state.replace('<state>','').replace('</state>','')

  postalCode = dom.getElementsByTagName('postalCode')[0].toxml()    
  postalCode = postalCode.replace('<postalCode>','').replace('</postalCode>','')

  patient = [firstName, lastName, street, city, state, postalCode]

  return patient


# Example path on windows
# path = 'C:\\Users\\mcotton\\Desktop\\xml'

path = './'
output = 'out.csv'

writer = csv.writer(open (output, 'wb'))

for infile in glob.glob( os.path.join( path, '*.*')):
  writer.writerow( pullInfo( infile) )

exit()

Yay! for python