Skip to content

Instantly share code, notes, and snippets.

@dare0021
Created May 18, 2017 04:43
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 dare0021/f524b5b1927fb6384bb589b22ac2063e to your computer and use it in GitHub Desktop.
Save dare0021/f524b5b1927fb6384bb589b22ac2063e to your computer and use it in GitHub Desktop.
# path of file to open
openFile = "stats.txt"
saveFile = openFile + ".json"
# returns string representations of items in array as a single string joined with joiner
# inserted between each item
def strJoin (sarr, joiner):
return joiner.join([str(x) for x in sarr])
def addItem (sBody, sLine):
sarr = sLine.split()
key = sarr[0]
value = sarr[1]
comment = strJoin(sarr[2:], ' ')
retval = sBody + '"' + key + '" : ' + value + ' ,\n'
retval += '"' + key + '_Comment" : "' + comment + '" ,\n'
return retval
retval = "{\n"
f = open(openFile, 'r')
# first line is always empty
f.readline()
if (f.readline() != "---------- Begin Simulation Statistics ----------\n"):
print "check input file path"
import sys
sys.exit()
s = f.readline()
while (len(s) > 1):
retval = addItem(retval, s)
s = f.readline()
f.close()
retval = retval[:len(retval)-2]
retval += "\n}"
sf = open(saveFile, 'w')
sf.write(retval)
sf.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment