1 def replace_star(actual, expect): 2 count = 0 3 for expect_char in expect: 4 count += 1 5 if expect_char == '*': # 遍历预期字符串,找到*位置 6 actual = actual[:count - 1] + "*" + actual[count:] # 把实际字符串该位置替换为* 7 for actual_char in actual[count:]: 8 if actual_char == '"': # 依次替换后面的字符为空,直到引号,引号可以换为其他字符,亦可多个 9 break 10 actual = actual[:count] + actual[count + 1:] 11 return actual 12 13 if __name__ == '__main__': 14 actual = '{"Status": 1, "Content": {"order_no": "20190813173142658002396779806585", "request_id": "4999870244479602310", "status": "SUCCESS"}, "Message": "请求成功"}' 15 expect = '{"Status": 1, "Content": {"order_no": "*", "request_id": "*", "status": "SUCCESS"}, "Message": "请求成功"}' 16 17 actual = replace_star(actual, expect) 18 print(actual)
两个字符串作对比,实际值一些no变化,预期值中用*代替