Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
straight forward solution solution in Clear category for YAML. More Types by nickgryg
import re
def yaml(a):
d = {}
for i in a.split('\n'):
if i:
if not i.split(':')[1] or i.split(': ')[1] == 'null':
d[i.split(':')[0]] = None
elif i.split(': ')[1] == 'false':
d[i.split(': ')[0]] = False
elif i.split(': ')[1] == 'true':
d[i.split(': ')[0]] = True
elif re.match('^\\d+$', i.split(': ')[1]):
d[i.split(': ')[0]] = int(i.split(': ')[1])
else:
d[i.split(': ')[0]] = i.split(': ')[1].replace('\\"', '@#$').replace('"', '').replace('@#$', '"').strip()
return d
if __name__ == '__main__':
print("Example:")
print(yaml('name: Alex\nage: 12'))
# These "asserts" are used for self-checking and not for an auto-testing
assert yaml('name: Alex\nage: 12') == {'age': 12, 'name': 'Alex'}
assert yaml('name: Alex Fox\n'
'age: 12\n'
'\n'
'class: 12b') == {'age': 12,
'class': '12b',
'name': 'Alex Fox'}
assert yaml('name: "Alex Fox"\n'
'age: 12\n'
'\n'
'class: 12b') == {'age': 12,
'class': '12b',
'name': 'Alex Fox'}
assert yaml('name: "Alex \\"Fox\\""\n'
'age: 12\n'
'\n'
'class: 12b') == {'age': 12,
'class': '12b',
'name': 'Alex "Fox"'}
assert yaml('name: "Bob Dylan"\n'
'children: 6\n'
'alive: false') == {'alive': False,
'children': 6,
'name': 'Bob Dylan'}
assert yaml('name: "Bob Dylan"\n'
'children: 6\n'
'coding:') == {'children': 6,
'coding': None,
'name': 'Bob Dylan'}
assert yaml('name: "Bob Dylan"\n'
'children: 6\n'
'coding: null') == {'children': 6,
'coding': None,
'name': 'Bob Dylan'}
assert yaml('name: "Bob Dylan"\n'
'children: 6\n'
'coding: "null" ') == {'children': 6,
'coding': 'null',
'name': 'Bob Dylan'}
print("Coding complete? Click 'Check' to earn cool rewards!")
Dec. 20, 2020