Last active
August 29, 2015 14:21
-
-
Save Spacecow99/1f088ce948061ce5a1cf to your computer and use it in GitHub Desktop.
Converts text file to json file
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 | |
| # | |
| # ToJSON - tojson.py - JSON conversion tool | |
| # | |
| # Takes a text file with content format: | |
| # #Comment | |
| # <Key> <Value>... | |
| # Converts it to JSON format and prints to | |
| # stdout so user can redirect output | |
| import sys | |
| def main(): | |
| print('{') | |
| with open(sys.argv[1], 'r') as f: | |
| for line in f.readlines(): | |
| if line[0] == '#': | |
| continue | |
| newline = '\t"' | |
| linearr = line.split() | |
| newline += linearr[0] + '": "' | |
| newline += ' '.join(linearr[1:]) | |
| newline = newline.strip('\n').strip(' ') + '",' | |
| print(newline) | |
| print('\t"FFFFFF": "Testing Testing"') # Test value | |
| print('}') | |
| if __name__ == "__main__": | |
| try: | |
| main() | |
| except(KeyboardInterrupt): | |
| print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment