orig_code = '''{ 'url': 'https://neighbors.ring.com/n/oEdj2', 'md5': '4f9038a4a926dea5db3d399d0326abe7', 'info_dict': { 'id': 'oEdj2', 'ext': 'mp4', 'title': 'Ding Dong Ditch is alive and well', 'thumbnail': 'https://neighbors.ring.com/assets/images/default_og_image.png', 'upload_date': '20180210', 'location': '4721 Indian Deer Rd, Windermere, FL 34786, USA', 'description': 'Get crime and safety alerts from your neighbors', } }''' def check_stack(look_for, stack): rstack = reversed(stack) for i, x in enumerate(rstack): if x == look_for: return raise SyntaxError('Unexpected {}'.format(look_for)) def conv(inp, indent_spaces=4): stack = [] is_dict = False is_key = False is_value = False seen_quote_open = False skip_1_space = False code = [] end = len(inp) - 1 indent = 0 level = 0 for i, c in enumerate(inp): if c == ':': continue if c == '\n': continue if c == ' ' and skip_1_space: if skip_1_space: skip_1_space = False continue if c == ',': code.append(c) if not is_value and level > 0: code.append('\n') indent += indent_spaces continue if c == '{': stack.append(c) is_dict = True is_key = True code.append('dict(') level += 1 continue if c == '}': check_stack('{', stack) code.append(')') is_dict = False if i != end: code.append(',\n') level -= 1 continue if is_key: if not seen_quote_open and c == ' ': continue if c in ('"', "'",): if not seen_quote_open: seen_quote_open = True stack.append(c) continue else: check_stack(c, stack) seen_quote_open = False is_key = False is_value = True c = '=' skip_1_space = True code.append(c) continue if is_value: if c in ('"', "'",): if not seen_quote_open: stack.append(c) seen_quote_open = True else: check_stack(c, stack) seen_quote_open = False is_value = False is_key = True code.append(c) continue return ''.join(code) print(conv(orig_code))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question