Created
January 16, 2020 15:03
-
-
Save ArthurGuy/027d186e1f5227fd516eaa0a5444eeba to your computer and use it in GitHub Desktop.
Convert a .env file to a json array
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
#!/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