import random import string def generate_uscc(): department_codes = ["1", "5", "9"] department_code = random.choice(department_codes) category_codes = ["1", "2", "3", "9"] category_code = random.choice(category_codes) administrative_division_codes = ["110000", "120000", "130000", "140000", "150000", "210000", "220000", "230000", "310000", "320000", "330000", "340000", "350000", "360000", "370000", "410000", "420000", "430000", "440000", "450000", "460000", "500000", "510000", "520000", "530000", "540000", "610000", "620000", "630000", "640000", "650000", "710000", "810000", "820000"] administrative_division_code = random.choice(administrative_division_codes) test_str = '0123456789ABCDEFGHJKLMNPQRTUWXY' organization_code = ''.join(random.sample(test_str + string.digits, 9)) sc =department_code + category_code + administrative_division_code + organization_code codeDict = { '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'J': 18, 'K': 19, 'L': 20, 'M': 21, 'N': 22, 'P': 23, 'Q': 24, 'R': 25, 'T': 26, 'U': 27, 'W': 28, 'X': 29, 'Y': 30}; weights = ['1', '3', '9', '27', '19', '26', '16', '17', '20', '29', '25', '13', '8', '24', '10', '30', '28']; sum = 0 for i in range(len(sc)): sum += codeDict[sc[i]] * int(weights[i]); mod = 31 - sum % 31 def getKey(codeDict, mod): result = '' for key in codeDict: if codeDict[key] == mod: result = key if (len(result) != 0): return result mod_2 = getKey(codeDict, mod) # print(mod_2) return department_code + category_code + administrative_division_code + organization_code + mod_2 license = generate_uscc() vars.put("licenseno_qy",license);
标签:脚本,代码生成,code,random,codes,自动化,codeDict,administrative,mod From: https://www.cnblogs.com/duroheng/p/17610778.html