Skip to content

Instantly share code, notes, and snippets.

@jkpl
Created Feb 25, 2017
Embed
What would you like to do?
Convert CSV to KeepassX XML format
import sys
entry_t = """<entry>
<title>{title}</title>
<username>{username}</username>
<password>{password}</password>
<notes>{info}</notes>
</entry>"""
pwfile_t = """<!DOCTYPE KEEPASSX_DATABASE>
<database>
<group>
<title>General</title>
%s
</group>
</database>"""
def get(l, i):
try:
return l[i]
except IndexError:
return ""
def convert_lines(lines):
converted = (convert_line(line) for line in lines)
joined = '\n'.join(converted)
return pwfile_t % joined
def convert_line(line):
l = line.split("\t")
d = {
'title': get(l, 0),
'username': get(l, 1),
'password': get(l, 2),
'info': get(l, 4),
}
return entry_t.format(**d)
def convert_file(filename):
with open(filename, 'r') as f:
print convert_lines(f)
if __name__ == '__main__':
convert_file(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment