Skip to content

Instantly share code, notes, and snippets.

@Kami
Created September 24, 2011 19:06
Show Gist options
  • Select an option

  • Save Kami/1239712 to your computer and use it in GitHub Desktop.

Select an option

Save Kami/1239712 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Convert 1Password .csv file to the KeePassX xml format
# Note: Ignores categories
import sys
import csv
import unicodedata
import lxml.etree as ET
# Maps fields names from 1Password to KeepPassX
FIELD_MAP = {
'title': 'title',
'username': 'username',
'password': 'password',
'URL/Location': 'url',
'notes': 'comment'
}
if len(sys.argv) != 3:
print 'Usage: ./convert <input 1Password file> <output keepassx file>'
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
with open(input_file, 'r') as fp:
reader = csv.DictReader(fp, delimiter='\t',
doublequote=True,
quoting=csv.QUOTE_ALL)
root = ET.Element('database')
group = ET.SubElement(root, 'group')
title = ET.SubElement(group, 'title')
title.text = 'Internet'
icon = ET.SubElement(group, 'icon')
icon.text = '1'
skipped = 0
for index, row in enumerate(reader):
entry = ET.SubElement(group, 'entry')
for name1, name2 in FIELD_MAP.items():
value = unicode(row[name1], 'utf-8')
if name1 == 'notes' and value == '\\n\\n':
value = u''
unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
item = ET.SubElement(entry, name2)
item.text = value
print 'Processed %d entries' % (index - skipped)
tree = ET.ElementTree(root)
tree.write(output_file)
print 'Written to %s' % (output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment