Skip to content

Instantly share code, notes, and snippets.

@jkpl
Created February 25, 2017 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkpl/435ef9a08119e98a04825a9aacfc674e to your computer and use it in GitHub Desktop.
Save jkpl/435ef9a08119e98a04825a9aacfc674e to your computer and use it in GitHub Desktop.
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