progressBefore = 0 def printProgressBar(percent, prefix = ''): global progressBefore if percent - progressBefore < 0.001: return progressBefore = percent percentStr = ("{0:.1f}").format(percent*100) filledLength = int(30 * percent) bar = '█' * filledLength + '-' * (30 - filledLength) print('\r%s |%s| %s%% ' % (prefix, bar, percentStr),end='') def checkID(id): if len(id) != 18: with open('error.txt','wt') as file: file.write("{}\n".format(id)) else: tmp = [] for i in id[0:17]: if not i.isdigit(): with open('error.txt','wt') as file: file.write("{}\n".format(id)) else: i = int(i) tmp.append(i) weights = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2] lZ = [0,1,2,3,4,5,6,7,8,9,10] lM = ['1','0','X','9','8','7','6','5','4','3','2'] sum = 0 for i in range(17): sum += tmp[i]*weights[i] Z = sum % 11 if lM[lZ.index(Z)] != id[17]: with open('error.txt','wt') as file: file.write("{}\n".format(id)) break f = open("ids.txt","rt") lines = f.readlines() for j in lines: jcopy = j.replace('\n','') checkID(jcopy) printProgressBar((lines.index(j)+1)/100000,prefix="Progress:")
时间: 2024-04-15 21:25:35 浏览: 148
自定义百分比的progressbar
5星 · 资源好评率100%
这段代码是一个用于检测身份证号码是否合法的程序。它首先定义了一个打印进度条的函数`printProgressBar`,然后定义了一个检测身份证号码的函数`checkID`。在主程序中,它从文件"ids.txt"中读取身份证号码,并逐个调用`checkID`函数进行检测,同时使用`printProgressBar`函数打印检测进度条。
具体来说,`checkID`函数会先检查身份证号码的长度是否为18位,如果不是,则将该身份证号码写入到文件"error.txt"中。如果长度为18位,则将身份证号码的前17位逐个转换为整数,并根据一定的权重进行计算。计算结果与身份证号码的第18位进行比较,如果不匹配,则同样将该身份证号码写入到文件"error.txt"中。
主程序则通过读取文件"ids.txt"逐行获取身份证号码,并调用`checkID`进行检测。同时,使用`printProgressBar`函数打印检测进度条,以显示检测的进度。
阅读全文