Skip to content

Instantly share code, notes, and snippets.

@ryanpersaud
Last active March 7, 2020 13:26
Show Gist options
  • Save ryanpersaud/7900c35677b40aa06c8aa29388a7be3b to your computer and use it in GitHub Desktop.
Save ryanpersaud/7900c35677b40aa06c8aa29388a7be3b to your computer and use it in GitHub Desktop.
Converts KeePass 1.x CSV to 1Password compatible CSV
# Adapted from https://stackoverflow.com/questions/51397083/python-3-6-rename-column-header-using-dictwriter
import csv
order = ['Account', 'Web Site', 'Login Name', 'Password', 'Comments', 'Member Number', 'Recovery Codes']
# define renamed columns via dictionary
renamer = {'Account': 'Title', 'Web Site' : 'Website', 'Login Name':'Username', 'Comments':'Notes'}
# define column names after renaming
new_cols = [renamer.get(x, x) for x in order]
# replace mystr as open(r'file.csv', 'r')
with open('password_keepass.csv', 'r') as fin, open('password_1password.csv', 'w', newline='') as fout:
# define reader / writer objects
reader = csv.DictReader(fin, delimiter=',')
writer = csv.writer(fout, delimiter=',', quoting=csv.QUOTE_ALL)
# write new header
writer.writerow(new_cols)
# iterate reader and write row
for item in reader:
writer.writerow([item.get(k, '') for k in order])
@ryanpersaud
Copy link
Author

quoting=csv.QUOTE_ALL may be unnecessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment