程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

身份证号码生成

发布于2019-08-17 20:04     阅读(1105)     评论(0)     点赞(5)     收藏(0)


Python 身份证号码生成

1、使用内置函数type(object)查看字符类型

len_l = randint(0, 3019)
print(len_l)
line = open("xingzhengquyu", encoding="utf-8").readlines()
print(line[len_l])
print(type('line[len_l]'))
  • 1
  • 2
  • 3
  • 4
  • 5

结果:
2341
511011 东兴区
<class ‘str’>

2、获取字符串中的数字

t=re.findall(r"\d+\.?\d*",line[len_l])
  • 1

3、zfill()日期位数不足,向左补齐

IDcard_dateyear=str(localtime(random_time).tm_year)
IDcard_datemonth=str(localtime(random_time).tm_mon)
if (len(IDcard_datemonth)==1):
    IDcard_datemonth=IDcard_datemonth.zfill(2)
IDcard_dateday=str(localtime(random_time).tm_mday)
if (len(IDcard_dateday)==1):
    IDcard_dateday = IDcard_dateday.zfill(2)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

说明:
Python zfill()方法返回指定长度的字符串,原字符串右对齐,前面填充0。
zfill()方法语法:str.zfill(width)
参数width – 指定字符串的长度。原字符串右对齐,前面填充0。
返回指定长度的字符串

结果:
time.struct_time(tm_year=1999, tm_mon=6, tm_mday=11, tm_hour=13, tm_min=5, tm_sec=26, tm_wday=4, tm_yday=162, tm_isdst=0)
465
19990611465

4、从行政区域划分行里取行政编码(622924 广河县)

print(111, line[len_l].split('	')[0])#以空格为界,分为两个list
print(112, line[len_l][:6]) #取前六个
  • 1
  • 2

5、split() 通过指定分隔符对字符串进行切片
文件中格式均为

110000	北京市
110101	东城区
110102	西城区
  • 1
  • 2
  • 3

顾利用空格为界,切片

area=111, line[len_l].split('	')[0])
  • 1

生成身份证整段:

# coding=utf-8
from random import *
from time import *
import pandas as pd
import numpy as np
import re
import os, sys
from datetime import date, timedelta

# 获取行政区域
len_l = randint(0, 3019)#当前共行政区划分3019
line = open("xingzhengquyu", encoding="utf-8").readlines()#读取整行
print('表中随机一行的值:',line[len_l])
# print(111, line[len_l].split('	')[0])#以空格为界,分为两个list
# print(112, line[len_l][:6]) #取前六个
# t=re.findall(r"\d+\.?\d*",line[len_l]) #正则表达式获取后,形式如:['530823'],还得再处理
IDcard_area=line[len_l][:6]

# 获取时间
date1=(1970,1,2,0,0,0,0,0,0)
time1=mktime(date1)
print(time1)
date2=(2019,6,4,0,0,0,0,0,0,)
time2=mktime(date2)
print(time2)
random_time =int(uniform(time1,time2))
print('打印随机时间戳',random_time)
print('转换格式后的值:',localtime(random_time))
IDcard_dateyear=str(localtime(random_time).tm_year)
IDcard_datemonth=str(localtime(random_time).tm_mon)
if (len(IDcard_datemonth)==1):
    IDcard_datemonth=IDcard_datemonth.zfill(2) #不足两位就补齐
IDcard_dateday=str(localtime(random_time).tm_mday)
if (len(IDcard_dateday)==1):
    IDcard_dateday = IDcard_dateday.zfill(2) #不足两位就补齐
    
#第15、16位数字表示:所在地的派出所的代码,第17位数字表示性别:奇数表示男性,偶数表示女性
IDcard_sex_police_station= str(randint(100, 999))
#前17位
IDcard=IDcard_area+IDcard_dateyear+IDcard_datemonth+IDcard_dateday+IDcard_sex_police_station
print('打印检查位之前(前17位)的值:',IDcard)

 # 算出校验码,第十八位
i = 0
count = 0
weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]  # 权重项
checkcode = {'0': '1', '1': '0', '2': 'X', '3': '9', '4': '8', '5': '7', '6': '6', '7': '5', '8': '5', '9': '3',
         '10': '2'}  # 校验码映射
for i in range(0, len(IDcard)):
    count = count + int(IDcard[i]) * weight[i]
IDcard = IDcard + checkcode[str(count % 11)]  # 算出校验码
print('生成的身份证信息为:',IDcard) #实际的身份证信息


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

发现日期生成有一种更加简单的方式,这种方式,不需要再年月日拼接
print(strftime("%Y%m%d", localtime(random_time)))

from time import *
# 获取时间
date1=(1970,1,2,0,0,0,0,0,0)
time1=mktime(date1)
print(time1)
date2=(2019,6,4,0,0,0,0,0,0,)
time2=mktime(date2)
print(time2)
random_time =int(uniform(time1,time2))
print('打印随机时间戳',random_time)
print('转换格式后的值:',localtime(random_time))
print(strftime("%Y%m%d", localtime(random_time)))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12


所属网站分类: 技术文章 > 博客

作者:239289

链接:https://www.pythonheidong.com/blog/article/48396/bb64355f50ffc58f94ae/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)