Skip to content

Instantly share code, notes, and snippets.

@f3rdy
Created January 13, 2018 11: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 f3rdy/06ef404b0d72f49ea64a78bf2055bf62 to your computer and use it in GitHub Desktop.
Save f3rdy/06ef404b0d72f49ea64a78bf2055bf62 to your computer and use it in GitHub Desktop.
How to use Python3 enum to access, index and transform a csv file
#!/usr/bin/env python3
from enum import Enum, unique
import sys, json
@unique
class Columns(Enum):
FOLDERNAME=0
FAVORITE=1
TYPE=2
NAME=3
NOTES=4
FIELDS=5
LOGIN_URI=6
LOGIN_USERNAME=7
LOGIN_PASSWORD=8
LOGIN_TOTP=9
def get_cred_from_line(line):
result = {}
for name, member in Columns.__members__.items():
result[name.lower()] = line[member.value]
return result
result = { 'data': [] }
with open(sys.argv[1], 'r') as f:
next(f) # skip header
result['data'] = [ get_cred_from_line(x.split(',')) for x in [ y.strip() for y in f.readlines() ]]
print(json.dumps(result, indent=4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment