-
-
Save kangasta/a0ad7a19e37be3621a7d2bf9fea7a876 to your computer and use it in GitHub Desktop.
Python dotenv parser
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
| # comment | |
| QUOTES="asd" | |
| INNER_QUOTES="{"asd": 123}" | |
| WHITESPACE= ws | |
| WHITESPACE_QUOTES= " ws " | |
| PLAIN=qwe | |
| EMPTY= | |
| # empty lines | |
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
| def read_dotenv(filename = '.env'): | |
| with open(filename) as f: | |
| items = (tuple(line.strip().split('=', 1)) for line in f.readlines() if line.strip() and not line.startswith('#')) | |
| return {item[0]: item[1].strip().strip('\'"') if len(item) > 1 else '' for item in items} | |
| if __name__ == '__main__': | |
| print(read_dotenv()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment