Last active
July 17, 2016 16:48
-
-
Save giampaolo44/0265895823ba358ed74223ded0e77af1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
import xml.etree.ElementTree as ET #import the xml library ElementTree | |
def getProperties(entry, ns): #function to find if it is being trashed and/or forwarded | |
for app_property in entry.findall('apps:property', ns): | |
name = app_property.attrib['name'] | |
if name in ('forwardTo', 'shouldTrash'): | |
yield name | |
def getAttribute(entry,ns,searched_attrib): #function to find given attributes of entries | |
string_attrib = 'apps:property[@name=\"'+ searched_attrib + '\"]' | |
return_attrib = entry.find(string_attrib, ns) | |
final_return_attrib = entry.find(string_attrib, ns).attrib['value'] if return_attrib != None else '' | |
return final_return_attrib | |
ns = { | |
'atom':'http://www.w3.org/2005/Atom', | |
'apps':'http://schemas.google.com/apps/2006', | |
} | |
tree = ET.parse('mailFilters.xml') #parse the gmail xml filter file | |
for index, entry in enumerate(tree.findall('atom:entry', ns)): #search entries and extract attributes | |
label = getAttribute(entry,ns,"label") | |
fromField = getAttribute(entry,ns,"from") | |
HTWField = getAttribute(entry,ns,"hasTheWord") | |
print('%d,%s,%s,%s,%s' %(index, fromField, HTWField, label, ','.join(getProperties(entry, ns)))) #print in csv format |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Look at https://docs.python.org/3/library/csv.html