Skip to content

Instantly share code, notes, and snippets.

@pddg
Created October 4, 2018 09:13
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 pddg/c3af6ce05a06484803659de757fe1b75 to your computer and use it in GitHub Desktop.
Save pddg/c3af6ce05a06484803659de757fe1b75 to your computer and use it in GitHub Desktop.
Help to migrate KeePass compatible database to 1Password
import argparse
import codecs
import csv
import pathlib
# Default: "Group","Title","Username","Password","URL","Notes"
ideal_header = ["Title", "URL", "Username", "Password", "Notes", "Group"]
def main():
parser = argparse.ArgumentParser(description="Make CSV exported from KeePassX compatible with 1Password.")
parser.add_argument('-f', '--file', type=str, nargs=1, help='Path to CSV')
args = parser.parse_args()
file_path = pathlib.Path(args.file[0]).resolve()
if not file_path.exists():
print('"{}" is not exists.'.format(file_path.as_posix()))
exit(1)
write_file_path = file_path.parent / (file_path.stem + '_modified' + file_path.suffix)
with codecs.open(file_path.as_posix(), 'r', 'utf-8') as fp:
reader = csv.reader(fp)
header = next(reader)
for row in reader:
row_dict = dict(zip(header, row))
with codecs.open(write_file_path.as_posix(), 'a', 'utf-8') as fp:
writer = csv.DictWriter(fp, lineterminator='\n', quoting=csv.QUOTE_ALL, fieldnames=ideal_header)
writer.writerow(row_dict)
print('Write to "{}"'.format(write_file_path.as_posix()))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment