ImportJsonwith Open ('Config.json') as F:result=json.loads (F.read ()) with open ('Testconfig.json','WB') as F:space_count=0 Last_char="' Current="'Is_in_double_quote=False forCurrent_charinchjson.dumps (Result): Last_char= Current Current=Current_charifIs_in_double_quote:ifCurrent_char = ='"': ifLast_char = ="\\": F.write (Current_char)Else: Is_in_double_quote= notis_in_double_quote f.write (Current_char)Else: F.write (Current_char)Else: ifCurrent_char = ='"': Is_in_double_quote= notis_in_double_quote f.write (Current_char)elifCurrent_char = ='{': Space_count+ = 1J=0 Current_char+='\ n' whileJ <Space_count:current_char+=' 'J+ = 1f.write (Current_char)elifCurrent_char = ='}': Space_count-= 1J=0 Add_space='\ n' whileJ <Space_count:add_space+=' 'J+ = 1Current_char= Add_space +Current_char f.write (Current_char)elifCurrent_char = =',': Current_char+='\ n'J=0 whileJ < Space_count-1: Current_char+=' 'J+ = 1F.write (Current_char+' ') Else: F.write (Current_char)
Ideas:
Reads the current character
1. First determine whether in the ' "'
1.1. In the ' "', determine if the current character is '" '
1.1.1. If not, write directly to the
1.1.2. If ' "', you need to determine if there is an escape character ' \ '
1.1.2.1. If there is one, write directly
1.1.2.2. Exit the ' ' status if not
1.2. Do not determine the current character type in ' "'
1.2.1. The current character is ' "', then go to '" ' state
1.2.2. The current character is ' {'} ', ' These three will be wrapped to add a space action
Json,dict Data Write automatic typesetting