Created
February 25, 2017 23:27
-
-
Save jpallari/435ef9a08119e98a04825a9aacfc674e to your computer and use it in GitHub Desktop.
Convert CSV to KeepassX XML format
This file contains hidden or 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
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