Created
May 18, 2017 04:43
-
-
Save jkihgit/f524b5b1927fb6384bb589b22ac2063e to your computer and use it in GitHub Desktop.
This file contains 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
# 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