-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprettyprint2.py
More file actions
46 lines (36 loc) · 1.2 KB
/
prettyprint2.py
File metadata and controls
46 lines (36 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
input = '{"a":"b","e":{"f":"g"},"c":"d"}'
''' desired output is:
{
"a":"b",
"e":{
"f":"g"
},
"c":"d"
}
'''
## The below doesn't quite work due to chars being repeated and it is not aware of indent levels, so it can't handle multiple nests
newoutput = ""
first_index = input.find('{')
second_index = input.find('{', first_index +1)
# # Loop through input string and add desired newlines and indents to the output var via concatenating
for index, char in enumerate(input):
if index == first_index:
newoutput += char + "\n" + " " * 2
if char == ',':
newoutput += char + "\n" + " " * 2
if index == second_index:
newoutput += char + "\n" + " " * 4
if char == '}':
newoutput += "\n" + char
else:
newoutput += char
print(newoutput)
# import re
# def find_nested_curly_braces(input):
# pattern = re.compile(r'(\{.*?(\{(.*?).*\})*?\})')
# matches = pattern.finditer(input)
# for match in matches:
# print(f"Found nested curly braces: {match.group(0)}")
# text = """This is an example {string with nested curly braces {also with curved braces } and more} stuff"""
# find_nested_curly_braces(input)
# # Create new output var and initialise with empty string