Skip to content

Instantly share code, notes, and snippets.

@Spacecow99
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save Spacecow99/1f088ce948061ce5a1cf to your computer and use it in GitHub Desktop.

Select an option

Save Spacecow99/1f088ce948061ce5a1cf to your computer and use it in GitHub Desktop.
Converts text file to json file
#!/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