For automated test cases, if 100 interfaces are to write 100 sheet forms in Excel, each interface has 10 fields, and 5 of them may be changing, you need to use parameterization, try using specific characters to position first, and replace placeholders when constructing parameters;
1. Replace the mobile phone number by using replace in the string
# Original string:{"mobilephone": "${not_existed_tel}", "pwd":"123456"} # json Character string src_str = '{"mobilephone": "${not_existed_tel}", "pwd":"123456"}' # replace json In string ${not_existed_tel} 18845820369 print(src_str.replace("${not_existed_tel}", "18845820369")) # Result:{"mobilephone": "18845820369", "pwd":"123456"}
2. Replace with re module
re regular expression is a formatting language for finding, searching, and replacing text
Search method one, match method in re module, match method is match from scratch
import re # 1. Create original string(String to replace) src_str4 = '{"mobilephone": "${not_existed_tel}", "pwd":"123456"}' # 2. Define pattern strings to match # Pattern string == Mod, use this template to compare in the original string # Method One, re Regular match(Translate: March) Method, match The method is to match from scratch # from { Start, don't match, Then reply None res = re.match("{not_existed_tel}", src_str4) print(res) # Result: None # from { Start, match up, Will return Match Object, plus r No escape required res = re.match(r'{"mobilephone"', src_str4) print(res) # Return Match Object Result:<re.Match object; span=(0, 14), match='{"mobilephone"'> # Get matching result content group (Translate: Songs are broken) a = res.group() print(a) # Result:{"mobilephone"
search Method 2: search method, matching only once
import re # 1. Create original string(String to replace) src_str1 = '{"mobilephone": "${not_existed_tel}", "pwd":"123456"}' # 2. Define pattern strings to match # Pattern string == Mod, use this template to compare in the original string:"${not_existed_tel}" # Method 2: search (Astringent) Method, match only once # Return if match Match object,Unmatched will not return None # Dollar sign needs escaping plus r,Escape is required for special characters res1 = re.search(r"\${not_existed_tel}", src_str1) print(res1) # Match Object Result:<re.Match object; span=(17, 35), match='${not_existed_tel}'> # Get matched results, group(Method b = res1.group() print(b) # Result: ${not_existed_tel}
Search method three: findall method, which matches many times
src_str1 = '{"mobilephone": "${not_existed_tel}", "pwd":"123456"}' res2 = re.findall(r'o', src_str1) print(res2) # Return list results directly:['o', 'o', 'o']
Replace method: sub method
import re # 1. Create original string(String to replace) src_str2 = '{"mobilephone": "${not_existed_tel}", "pwd":"123456"}' # The first parameter is the pattern string: ${not_existed_tel} # The second parameter is the new string: 18845820369 # The third parameter is the original string # sub Method if it matches, Then the replaced string is returned # sub Method if not matched, Then return the original string directly # Use search Method searches first, searches then uses sub Method to replace; count=0 Default Match Once if re.search(r"\${not_existed_tel}", src_str2): # Search first res2 = re.sub(r"\${not_existed_tel}", "18845820369", src_str2) # In Replacement print(res2) else: print("Unable to match original string") # Replacement results:{"mobilephone": "18845820369", "pwd":"123456"} # Use regular matching:\w+ Word Characters[A-Za-z0-9_],+ Match multiple times; match in the original string by regular if re.search(r"\${\w+}", src_str2): # Search first res2 = re.sub(r"\${not_existed_tel}", "18845820369", src_str2) # In Replacement print(res2) else: print("Unable to match original string") # Replacement results:{"mobilephone": "18845820369", "pwd":"123456"}
******* Please respect the original. If you want to reprint it, please indicate the source: reprinted from: https://www.cnblogs.com/shouhu/ Thank you!*******