Reading xml: Searching the sanctions lists

This programme searches the US Treasury and UN Security Council sanctions lists by nationality and returns the total number of listed individuals.

The programme defines a function for reading the lists (in xml format), then asks for user input on 1) which list the user wishes to search and 2) what country the user would like to search for. It then tallies the total number of individuals from each country on the requested list.

I’ve also done variations on this, such as pulling the names themselves from the list or creating a table with all nationalities. (I’m still working through the names programme though. The UN list has up to four names for some individuals, and building it so it grabs all of them in the right order is taking some time.)

The sources can be found here:

import xml.etree.ElementTree as ET

def terrlstsearch(src, srcsearch1, srcsearch2, ctry):

count = 0

tree = ET.parse(src)

all = tree.findall(srcsearch1)

for entry in all :

#Try / except used because not all entries have nationalities (ie some listed entities are companies rather than individuals, some individuals are stateless)

try :

if entry.find(srcsearch2).text == ctry :

count = count + 1

except :


print ”

if count == 0 :

print ‘There are’, count, ‘sanctioned individuals from’, ctry

if count == 1 :

print ‘There is 1 sanctioned individual from’, ctry

if count > 1 :

print ‘There are’, count, ‘sanctioned individuals from’, ctry

return ”

jump = ‘go’

while jump == ‘go’ :

print ”

src = input(‘Enter list name (US or UN): ‘)

ctry = input(‘Enter name of country: ‘)

if src == ‘US’:

src = ‘sdn1.xml’

srcsearch1 = ‘sdnEntry’

srcsearch2 = ‘nationalityList/nationality/country’

first = ‘firstName’

last = ‘lastName’

elif src == ‘UN’:

if ctry == ‘United Kingdom’ :

ctry = ‘United Kingdom of Great Britain and Northern Ireland’

# The UN lists the UK’s full name. I’m sure there’s others like this but I haven’t listed out the exceptions or cleaned the xml file.

src = ‘UNconsolidated.xml’


srcsearch2 = ‘NATIONALITY/VALUE’

print terrlstsearch(src, srcsearch1, srcsearch2, ctry)

jump = input(‘Enter go to search again or stop to stop: ‘)

print ”
print ‘Exit’
print ”



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s