import json import codecs import re file_path = r'd:\\骅锋\\mall\\pages.json' with codecs.open(file_path, 'r', 'utf-8') as f: text = f.read() target = r'(\{\s*\ path\\s*:\s*\pages/mall/admin/homePage/index\[\w\W]*?\})' new_block = ''',{ \path\: \pages/mall/admin/userCenter/index\, \style\: { \navigationBarTitleText\: \个人中心\, \navigationStyle\: \custom\ } }''' def rcb(match): return match.group(1) + new_block new_text = re.sub(target, rcb, text) if new_text != text: with codecs.open(file_path, 'w', 'utf-8') as f: f.write(new_text) print('Updated!') else: print('No changes!')