Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- def open_file(path_to_file):
- return open(path_to_file)
- def parse(file):
- line_inner = 0
- json_data = {}
- last_header = None
- main_header = None
- under_header = None
- # Lines
- for line in file.readlines():
- # Line
- for s in line:
- if s == ' ':
- line_inner += 1
- else:
- break
- data = line[line_inner:]
- # HEADER
- if len(data.split(' ')) == 1:
- if line_inner == 0:
- main_header = data[:-2]
- json_data[data[:-2]] = {}
- elif line_inner == 2:
- under_header = data[:-2]
- json_data[main_header][data[:-2]] = {}
- else:
- last_header = data[:-2]
- json_data[main_header][under_header][data[:-2]] = {}
- # DATA
- else:
- key = data.split(':')[0]
- if len(data.split(':')[1]) <= 2:
- json_data[main_header][under_header][last_header][key] = ''
- else:
- value = ''.join(data.split(':')[1:])[2:-2]
- new_value = data.split(':', maxsplit=1)[1].replace('"', '')[1:-1]
- json_data[main_header][under_header][last_header][key] = new_value
- line_inner = 0
- with open('data.json', 'w', encoding='utf-8') as file:
- iter = 0
- for k, v in json_data.items():
- if iter == 0:
- k = '{}'.format(k)
- k = "{'" + k + "'"
- iter+=1
- file.write('{}:{}\n'.format(k.replace("'", '"'), str(v).replace("'", '"')))
- file.write('}')
- parse(open_file('data.yml'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement