Monday, 25 August 2014

Parsing XML using Python library



I am trying to create a Python script to create a table according to the policy number in the XML file. Just a background, there is a policy which has children as composite key etc. So there are different composite keys in the file i want to increment whenever there is a actioncode A and Decrement whenever there is actioncode of D. So i came with the following script.



import xml.etree.ElementTree as ET
f = open('delta-stats.txt','w')
tree = ET.parse('tester.xml')
root = tree.getroot()
dict = { }
countI=0
for party in root.findall('party'):
if(party[0].text == 'A'):
if party.findall('.//roleCode')[0].text == 'I':
countI= countI+1
if(party[0].text == 'D'):
if party.findall('.//roleCode')[0].text == 'I':
countI= countI-1
print(party.find('./compositeKey/collectionOfficeCode').text +',' + party.find('./compositeKey/policyNumber').text + ',' + str(countI))
f.close()


So now my problem with the code is how would i distinguish between the different polices and increment the counter according.


Current output is



0804,9876543210,2


Expected output



0804,1876543210,1
0804,9876543210,2


Here is the XML:



<parties>
<!-- Father Brown - Bene -->
<party>
<actionCode>A</actionCode>

<compositeKey>
<collectionOfficeCode>0804</collectionOfficeCode>
<coverageCode></coverageCode>
<coverageTypeDescription></coverageTypeDescription>
<customerID></customerID>
<employeeNumber></employeeNumber>
<groupNumber></groupNumber>
<policyNumber>1876543210</policyNumber>
<policySuffixCode></policySuffixCode>
</compositeKey>

<birthDayNumber>01</birthDayNumber>
<birthMonthNumber>04</birthMonthNumber>
<birthYearNumber>1983</birthYearNumber>
<birthdate>1983-04-01 00:00:00.0</birthdate>

<contactMethod>
<contact>
<addressLineOne>MetLife Alico</addressLineOne>
<addressLineThree>23 Main Street</addressLineThree>
<addressLineTwo>Suite 300E</addressLineTwo>
<addressTypeDescription>Home</addressTypeDescription>
<cityName>Athens</cityName>
<countryName>GREECE</countryName>
<stateCode></stateCode>
<typeDescription>ADDRESS</typeDescription>
<zipCode>09740</zipCode>
<addressMatchCode>V1</addressMatchCode>
</contact>
<contact>
<telephoneCountryNumber>0030</telephoneCountryNumber>
<telephoneAreaNumber>555</telephoneAreaNumber>
<telephonePartialNumber>888888888</telephonePartialNumber>
<telephoneTypeDescription>Mobile</telephoneTypeDescription>
<telephoneExtensionNumber></telephoneExtensionNumber>
<typeDescription>PHONE</typeDescription>
</contact>
<contact>
<emailAddressLine>father@metlife.com</emailAddressLine>
<preferredEmailAddressIndicator>Y</preferredEmailAddressIndicator>
<typeDescription>EMAIL</typeDescription>
</contact>
</contactMethod>

<employerIDNumber></employerIDNumber>
<employerIDNumberFirstFive></employerIDNumberFirstFive>
<employerIDNumberLastFour></employerIDNumberLastFour>

<firstName>Dad</firstName>
<rootFirstName>Father</rootFirstName>
<generationOfName>Sr.</generationOfName>
<lastName>Brown</lastName>
<middleOneName></middleOneName>
<rootMiddleName></rootMiddleName>
<rootOrganizationName></rootOrganizationName>
<suffixOfName></suffixOfName>

<policyRoles>
<policyRole>
<ownerRoleDescription></ownerRoleDescription>
<roleCode>B</roleCode>
<roleDescription>Beneficiary</roleDescription>
</policyRole>
</policyRoles>

<organizationEstablishedDate></organizationEstablishedDate>

<riders>
</riders>

<taxID>333333333</taxID>
<taxIDFirstFive>33333</taxIDFirstFive>
<taxIDLastFour>3333</taxIDLastFour>

<typeCode>I</typeCode>

</party>

No comments:

Post a Comment