Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for YAML. More Types by tokiojapan55
def yaml(a):
result = []
cls = dict()
for line in a.splitlines():
line = line.strip()
if len(line) > 0 and line[0] == '-':
token = line[1:].strip()
if len(token) <= 0:
result.append(None)
else:
s = 0
if token[0] == '"':
s = token[1:].find('"')
c = token[s:].find('#')
if c >= 0:
token = token[s:][:c]
token = token.strip()
token = token.strip('"')
if len(token) <= 0:
result.append(None)
elif token.isdigit():
result.append(int(token))
else:
result.append(token)
else:
b = line.split(':')
if len(b) == 2:
atr = b[0].strip()
val = b[1].strip()
if val.isdigit():
cls[atr] = int(val)
elif val == 'false':
cls[atr] = False
elif val == 'true':
cls[atr] = True
elif val == 'null' or len(val) == 0:
cls[atr] = None
else:
if len(val) >= 2 and val[0] == '"' and val[-1] == '"':
val = val[1:-1]
if val.find('\\') >= 0:
val = ''.join([c for c in list(val) if c != '\\'])
cls[atr] = val
print(result, cls)
if result:
return result
else:
return cls
June 17, 2020