Skip to content

Instantly share code, notes, and snippets.

@drewkerrigan
Last active December 15, 2015 18:39
Show Gist options
  • Select an option

  • Save drewkerrigan/5305903 to your computer and use it in GitHub Desktop.

Select an option

Save drewkerrigan/5305903 to your computer and use it in GitHub Desktop.
riak-admin status to json
import re
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def status_to_json(raw_stats):
# remove all unnecessary symbols and whitespace
stats = ""
for line in raw_stats.splitlines():
stats += line.strip() + '\n'
stats = stats.replace('<<', '').replace('>>', '')
stats = stats.replace('\\n', '').replace(',\n', ',')
stats = stats.replace(": '",': ').replace("'\n","\n")
stats = stats.replace(': "[', ': [').replace(']"\n',"]\n")
stats = stats.replace('"',"'")
matchObj = re.compile(r"^(.*) : (.*)", re.M|re.I)
json_stats = '{'
for match in matchObj.finditer(stats):
key,value = match.groups()
if (value[0] == "'"):
value = value[1:-1]
if not is_number(value):
value = '"' + value + '"'
json_stats += '"' + key + '":' + value + ','
json_stats = json_stats[0:-1]
json_stats += '}'
return json_stats
f = open('test.txt', 'r')
print status_to_json(f.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment