两个字符串作对比,实际值一些no变化,预期值中用*代替,以下是脚本
actual = '{"Status": 1, "Content": {"order_no": "20190813173142658002396779806585", "request_id": "4999870244479602310", "status": "SUCCESS"}, "Message": "请求成功"}' expect = '{"Status": 1, "Content": {"order_no": "*", "request_id": "*", "status": "SUCCESS"}, "Message": "请求成功"}' count = 0 for expect_char in expect: count += 1 if expect_char == '*': # 遍历预期字符串,找到*位置 print(count) actual = actual[:count - 1] + "*" + actual[count:] # 把实际字符串该位置替换为* print(actual) for actual_char in actual[count:]: if actual_char == '"': # 依次替换后面的字符为空,直到引号,引号可以换为其他字符,亦可多个 break actual = actual[:count] + actual[count + 1:] print(actual, count) print(actual)
因为对性能要求不高,想提高性能,可替换字符串拼接方式。