Skip to content

Instantly share code, notes, and snippets.

@ArthurGuy
Created January 16, 2020 15:03
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 ArthurGuy/027d186e1f5227fd516eaa0a5444eeba to your computer and use it in GitHub Desktop.
Save ArthurGuy/027d186e1f5227fd516eaa0a5444eeba to your computer and use it in GitHub Desktop.
Convert a .env file to a json array
#!/usr/bin/env python
import json
import sys
try:
dotenv = sys.argv[1]
except IndexError as e:
dotenv = '.env'
with open(dotenv, 'r') as f:
content = f.readlines()
# removes whitespace chars like '\n' at the end of each line
content = [x.strip().split('=', 1) for x in content if '=' in x]
vars={}
for x in content:
vars[x[0].strip()] = x[1].strip()
print(json.dumps(dict(vars)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment