Skip to content

Instantly share code, notes, and snippets.

@a9QrX3Lu
Last active August 15, 2021 06:05
Show Gist options
  • Select an option

  • Save a9QrX3Lu/1fb7c862bd136e848ad4d760f68583f5 to your computer and use it in GitHub Desktop.

Select an option

Save a9QrX3Lu/1fb7c862bd136e848ad4d760f68583f5 to your computer and use it in GitHub Desktop.
Transform field list like into json object. Field list looks like https://docs.ceph.com/en/latest/radosgw/lua-scripting/#id6.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
with open("temp.txt", 'r') as f:
lines = f.readlines()
output = []
for line in lines:
if line.startswith("Request."):
output.append(line)
request = {}
for line in output:
fields = line.split('.')
fields = [f.strip() for f in fields[1:]]
curr = request
last_field = ""
for field in fields:
if isinstance(curr, str):
last[last_field] = {field: {}}
curr = last[last_field]
elif field not in curr:
curr[field] = {}
last = curr
last_field = field
curr = curr[field]
last[last_field] = line.strip()
json_dump = json.dumps(request)
json_str = str(json_dump)
results = []
for word in json_str.split(' '):
new_word = word
if '"' in new_word:
new_word = new_word.replace('"', '')
if ':' in new_word:
new_word = new_word.replace(':', ' =')
results.append(new_word)
print(' '.join(results))
with open("temp1.txt", 'w') as f:
f.writelines(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment