#这里数字转换主要是为了解决网站返回虚假数据的问题,但写完才发现网站的数字是有自己的一套机制的,而不是固定的,因此不好解决,那就当回顾一下re匹配知识吧。
1 str="1332-31-15" 2 3 4 # print(str[:-5]) 5 # print(str1[:-5]) 6 7 8 import re 9 10 11 # print(A) 12 # print(type(A)) 13 14 def trans(A): 15 num=[] 16 for i in range (0,len(A)): 17 if A[i]=="0": 18 zero=re.compile("0") 19 z=re.sub(zero,"5",A[i]) 20 num.append(z) 21 22 elif A[i]=="1": 23 one=re.compile("1") 24 o=re.sub(one,"2",A[i]) 25 # print(o,i) 26 num.append(o) 27 28 elif A[i]=="2": 29 two=re.compile("2") 30 t=re.sub(two,"9",A[i]) 31 # print(t,i) 32 num.append(t) 33 34 elif A[i]=="3": 35 three=re.compile("3") 36 t2=re.sub(three,"0",A[i]) 37 # print(t2,i) 38 num.append(t2) 39 40 elif A[i]=="4": 41 four=re.compile("4") 42 f=re.sub(four,"6",A[i]) 43 # print(f,i) 44 num.append(f) 45 46 elif A[i]=="5": 47 five=re.compile("5") 48 f2=re.sub(five,"4",A[i]) 49 # print(f2,i) 50 num.append(f2) 51 52 elif A[i]=="6": 53 six=re.compile("6") 54 s=re.sub(six,"7",A[i]) 55 # print(s,i) 56 num.append(s) 57 58 elif A[i]=="7": 59 seven=re.compile("7") 60 s2=re.sub(seven,"3",A[i]) 61 # print(s2,i) 62 num.append(s2) 63 64 elif A[i]=="8": 65 eight=re.compile("8") 66 e=re.sub(eight,"1",A[i]) 67 # print(e,i) 68 num.append(e) 69 70 elif A[i]=="9": 71 nine=re.compile("9") 72 n=re.sub(nine,"8",A[i]) 73 # print(n,i) 74 num.append(n) 75 # print(num) 76 77 number=''.join(num) 78 return number 79 80 81 year=str[:4] 82 y=trans(year) 83 mon=str[5:7] 84 m=trans(mon) 85 day=str[8:10] 86 d=trans(day) 87 88 print(y+'-'+m+'-'+d)