python操作redis之随机生成18位身份证号码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python操作redis之随机生成18位身份证号码相关的知识,希望对你有一定的参考价值。

写一个随机生成身份证号的程序,输入多少条就产生多少个,?把产生完的数据写到redis里面,key用哈希类型?Xiaohei 410881198312031241

其中配置文件confsetting.py里的常量有:

# 系统常量,包含:
# LAST_NAME: 姓
# FIRST_NAME: 名
# STATE_CODE:全国大陆地区及编码
REDIS_IP = 172.16.2.163‘ RedisIp
REDIS_PORT = 6379
REDIS_PASSWD = 222333
REDIS_DB = 1
LAST_NAME = [, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ‘闻人, 东方, 赫连, 皇甫, 尉迟, 公羊, 澹台, 公冶, 宗政, 濮阳, 淳于, 单于, 太叔, 申屠, 公孙, 仲孙, 轩辕, 令狐, 钟离, 宇文, 长孙, 慕容, 司徒, 司空, , , , 叶赫那拉, , , , , , , , , , , , , , , , , , , , , , , , , , , 章佳, 那拉, , , , , 依尔根觉罗, 依尔觉罗, 萨嘛喇, 赫舍里, 额尔德特, 萨克达, 钮祜禄, 他塔喇, 喜塔腊, 讷殷富察, 叶赫那兰, 库雅喇, 瓜尔佳, 舒穆禄, 爱新觉罗, 索绰络, 纳喇, 乌雅, 范姜, 碧鲁, 张廖, 张简, 图门, 太史, 公叔, 乌孙, 完颜, 马佳, 佟佳, 富察, 费莫, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , 第五, , , , , , , , 南宫, , , , , , , 西门, 东门, 左丘, 梁丘, , , , , , , 微生, 羊舌, , , 呼延, 南门, 东郭, 百里, , , , , , , , 谷梁, 宰父, 夹谷, 拓跋, 壤驷, 乐正, 漆雕, 公西, 巫马, 端木, 颛孙, 子车, , , 司寇, 亓官, 鲜于, 锺离, , , , , , , , , , 闾丘, 公良, 段干, , , , , , , , , , ]

FIRST_NAME = [ , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , 使, , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , 西, , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , 便, , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , 穿, , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , 怀,
               , , 广, , , , , , , , , , , , , , , , , ,
               , , , , 线, , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               退, , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,
               , , , , , , , , , , , , , , , , , , , ,, , ‘‘]

STATE_CODE=[{state: 北京市市辖区, code: 110100}, {state: 北京市东城区, code: 110101}, {state: 北京市西城区, code: 110102}, {state: 北京市崇文区, code: 110103}, {state: 北京市宣武区, code: 110104}, {state: 北京市朝阳区, code: 110105}, {state: 北京市丰台区, code: 110106}, {state: 北京市石景山区, code: 110107}, {state: 北京市海淀区, code: 110108}, {state: 北京市门头沟区, code: 110109}, {state: 北京市房山区, code: 110111}, {state: 北京市通州区, code: 110112}, {state: 北京市顺义区, code: 110113}, {state: 北京市县, code: 110200}, {state: 北京市昌平县, code: 110221}, {state: 北京市大兴县, code: 110224}, {state: 北京市平谷县, code: 110226}, {state: 北京市怀柔县, code: 110227}, {state: 北京市密云县, code: 110228}, {state: 北京市延庆县, code: 110229}, {state: 天津市市辖区, code: 120100}, {state: 天津市和平区, code: 120101}, {state: 天津市河东区, code: 120102}, {state: 天津市河西区, code: 120103}, {state: 天津市南开区, code: 120104}, {state: 天津市河北区, code: 120105}, {state: 天津市红桥区, code: 120106}, {state: 天津市塘沽区, code: 120107}, {state: 天津市汉沽区, code: 120108}, {state: 天津市大港区, code: 120109}, {state: 天津市东丽区, code: 120110}, {state: 天津市西青区, code: 120111}, {state: 天津市津南区, code: 120112}, {state: 天津市北辰区, code: 120113}, {state: 天津市县, code: 120200}, {state: 天津市宁河县, code: 120221}, {state: 天津市武清县, code: 120222}, {state: 天津市静海县, code: 120223}, {state: 天津市宝坻县, code: 120224}, {state: 天津市蓟县, code: 120225}, {state: 河北省石家庄市, code: 130100}, {state: 河北省石家庄市市辖区, code: 130101}, {state: 河北省石家庄市长安区, code: 130102}, {state: 河北省石家庄市桥东区, code: 130103}, {state: 河北省石家庄市桥西区, code: 130104}, {state: 河北省石家庄市新华区, code: 130105}, {state: 河北省石家庄市郊区, code: 130106}, {state: 河北省石家庄市井陉矿区, code: 130107}, {state: 河北省石家庄市井陉县, code: 130121}, {state: 河北省石家庄市正定县, code: 130123}, {state: 河北省石家庄市栾城县, code: 130124}, {state: 河北省石家庄市行唐县, code: 130125}, {state: 河北省石家庄市灵寿县, code: 130126}, {state: 河北省石家庄市高邑县, code: 130127}, {state: 河北省石家庄市深泽县, code: 130128}, {state: 河北省石家庄市赞皇县, code: 130129}, {state: 河北省石家庄市无极县, code: 130130}, {state: 河北省石家庄市平山县, code: 130131}, {state: 河北省石家庄市元氏县, code: 130132}, {state: 河北省石家庄市赵县, code: 130133}, {state: 河北省石家庄市辛集市, code: 130181}, {state: 河北省石家庄市藁城市, code: 130182}, {state: 河北省石家庄市晋州市, code: 130183}, {state: 河北省石家庄市新乐市, code: 130184}, {state: 河北省石家庄市鹿泉市, code: 130185}, {state: 河北省唐山市, code: 130200}, {state: 河北省唐山市市辖区, code: 130201}, {state: 河北省唐山市路南区, code: 130202}, {state: 河北省唐山市路北区, code: 130203}, {state: 河北省唐山市古冶区, code: 130204}, {state: 河北省唐山市开平区, code: 130205}, {state: 河北省唐山市新区, code: 130206}, {state: 河北省唐山市丰润县, code: 130221}, {state: 河北省唐山市滦县, code: 130223}, {state: 河北省唐山市滦南县, code: 130224}, {state: 河北省唐山市乐亭县, code: 130225}, {state: 河北省唐山市迁西县, code: 130227}, {state: 河北省唐山市玉田县, code: 130229}, {state: 河北省唐山市唐海县, code: 130230}, {state: 河北省唐山市遵化市, code: 130281}, {state: 河北省唐山市丰南市, code: 130282}, {state: 河北省唐山市迁安市, code: 130283}, {state: 河北省秦皇岛市秦皇岛市, code: 130300}, {state: 河北省秦皇岛市市辖区, code: 130301}, {state: 河北省秦皇岛市海港区, code: 130302}, {state: 河北省秦皇岛市山海关区, code: 130303}, {state: 河北省秦皇岛市北戴河区, code: 130304}, {state: 河北省秦皇岛市青龙满族自治县, code: 130321}, {state: 河北省秦皇岛市昌黎县, code: 130322}, {state: 河北省秦皇岛市抚宁县, code: 130323}, {state: 河北省秦皇岛市卢龙县, code: 130324}, {state: 河北省邯郸市邯郸市, code: 130400}, {state: 河北省邯郸市市辖区, code: 130401}, {state: 河北省邯郸市邯山区, code: 130402}, {state: 河北省邯郸市丛台区, code: 130403}, {state: 河北省邯郸市复兴区, code: 130404}, {state: 河北省邯郸市峰峰矿区, code: 130406}, {state: 河北省邯郸市邯郸县, code: 130421}, {state: 河北省邯郸市临漳县, code: 130423}, {state: 河北省邯郸市成安县, code: 130424}, {state: 河北省邯郸市大名县, code: 130425}, {state: 河北省邯郸市涉县, code: 130426}, {state: 河北省邯郸市磁县, code: 130427}, {state: 河北省邯郸市肥乡县, code: 130428}, {state: 河北省邯郸市永年县, code: 130429}, {state: 河北省邯郸市邱县, code: 130430}, {state: 河北省邯郸市鸡泽县, code: 130431}, {state: 河北省邯郸市广平县, code: 130432}, {state: 河北省邯郸市馆陶县, code: 130433}, {state: 河北省邯郸市魏县, code: 130434}, {state: 河北省邯郸市曲周县, code: 130435}, {state: 河北省邯郸市武安市, code: 130481}, {state: 河北省邢台市, code: 130500}, {state: 河北省邢台市市辖区, code: 130501}, {state: 河北省邢台市桥东区, code: 130502}, {state: 河北省邢台市桥西区, code: 130503}, {state: 河北省邢台市邢台县, code: 130521}, {state: 河北省邢台市临城县, code: 130522}, {state: 河北省邢台市内丘县, code: 130523}, {state: 河北省邢台市柏乡县, code: 130524}, {state: 河北省邢台市隆尧县, code: 130525}, {state: 河北省邢台市任县, code: 130526}, {state: 河北省邢台市南和县, code: 130527}, {state: 河北省邢台市宁晋县, code: 130528}, {state: 河北省邢台市巨鹿县, code: 130529}, {state: 河北省邢台市新河县, code: 130530}, {state: 河北省邢台市广宗县, code: 130531}, {state: 河北省邢台市平乡县, code: 130532}, {state: 河北省邢台市威县, code: 130533}, {state: 河北省邢台市清河县, code: 130534}, {state: 河北省邢台市临西县, code: 130535}, {state: 河北省邢台市南宫市, code: 130581}, {state: 河北省邢台市沙河市, code: 130582}, {state: 河北省保定市, code: 130600}, {state: 河北省保定市市辖区, code: 130601}, {state: 河北省保定市新市区, code: 130602}, {state: 河北省保定市北市区, code: 130603}, {state: 河北省保定市南市区, code: 130604}, {state: 河北省保定市满城县, code: 130621}, {state: 河北省保定市清苑县, code: 130622}, {state: 河北省保定市涞水县, code: 130623}, {state: 河北省保定市阜平县, code: 130624}, {state: 河北省保定市徐水县, code: 130625}, {state: 河北省保定市定兴县, code: 130626}, {state: 河北省保定市唐县, code: 130627}, {state: 河北省保定市高阳县, code: 130628}, {state: 河北省保定市容城县, code: 130629}, {state: 河北省保定市涞源县, code: 130630}, {state: 河北省保定市望都县, code: 130631}, {state: 河北省保定市安新县, code: 130632}, {state: 河北省保定市易县, code: 130633}, {state: 河北省保定市曲阳县, code: 130634}, {state: 河北省保定市蠡县, code: 130635}, {state: 河北省保定市顺平县, code: 130636}, {state: 河北省保定市博野县, code: 130637}, {state: 河北省保定市雄县, code: 130638}, {state: 河北省保定市涿州市, code: 130681}, {state: 河北省保定市定州市, code: 130682}, {state: 河北省保定市安国市, code: 130683}, {state: 河北省保定市高碑店市, code: 130684}, {state: 河北省张家口市, code: 130700}, {state: 河北省张家口市市辖区, code: 130701}, {state: 河北省张家口市桥东区, code: 130702}, {state: 河北省张家口市桥西区, code: 130703}, {state: 河北省张家口市宣化区, code: 130705}, {state: 河北省张家口市下花园区, code: 130706}, {state: 河北省张家口市宣化县, code: 130721}, {state: 河北省张家口市张北县, code: 130722}, {state: 河北省张家口市康保县, code: 130723}, {state: 河北省张家口市沽源县, code: 130724}, {state: 河北省张家口市尚义县, code: 130725}...]这就不全部列出来了

代码主体:

 

import time, random,string,redis,os,sys
cur_path = os.path.abspath(__file__)
base_path = os.path.dirname(os.path.dirname(cur_path))
sys.path.insert(0,base_path)
from conf.confsetting import FIRST_NAME,LAST_NAME,STATE_CODE,REDIS_IP,REDIS_PORT,REDIS_PASSWD,REDIS_DB
def get_bornDate():
    ‘‘‘
    返回一个随机生成的‘19880914‘这样格式的日期
    ‘‘‘
    start_date = time.mktime(time.strptime(1930-01-01 00:00:00, %Y-%m-%d %H:%M:%S))
    end_date = int(time.time())
    born_time = random.randint(start_date, end_date)  # 在开始和结束时间戳中随机取出一个时间戳
    date_touple = time.localtime(born_time)  # 时间戳转时间元组
    random_date = time.strftime("%Y%m%d", date_touple)  # 时间元组转成格式化字符串
    return random_date

def get_IdCard(u_card):
    ‘‘‘
    返回一个包含某个地区的18位身份证号和地区名称的元组(idcard,state)
    ‘‘‘
    src_num = string.digits
    src_c = string.digits + X
    dic = STATE_CODE[random.randint(0, len(STATE_CODE))]
    idcard = dic[code] + get_bornDate() + ‘‘.join(random.sample(src_num, 3)) + ‘‘.join(random.sample(src_c, 1))
    if idcard in u_card: #如果生成的身份证redis已存在,则重新生成
        get_IdCard(u_card)
    else:
        return idcard

def get_name():
    ‘‘‘
    返回一个随机生成的姓名
    ‘‘‘
    l_name = LAST_NAME[random.randint(0,len(LAST_NAME))]
    #firstname随机取1-2个汉子
    count = random.randint(1,2)
    if count == 1:
        f_name = FIRST_NAME[random.randint(0,len(FIRST_NAME))]
    else:
        f_name = FIRST_NAME[random.randint(0,len(FIRST_NAME))]+FIRST_NAME[random.randint(0,len(FIRST_NAME))]
    name = l_name + f_name
    return name

count = input("请输入你要生成名字和身份证的数量:\\n").strip()
for i in range(int(count)):
    r = redis.Redis(host=REDIS_IP, port=REDIS_PORT, password=REDIS_PASSWD, db=REDIS_DB, socket_timeout=10,
                    socket_connect_timeout=10)  # 获取数据
    name = get_name()
    user = r.hgetall(user)
    u_card = []
    for k in user:
        u_card.append(user[k].decode())
    new_id = get_IdCard(u_card)
    r.hset(user, name, new_id)

运行结果:

技术分享

 

以上是关于python操作redis之随机生成18位身份证号码的主要内容,如果未能解决你的问题,请参考以下文章

python Python中随机生成身份证号/中文名

身份证号生成,校验码计算

如何在数据库中利用sql从18位身份证号中截取出生日期,身份证号在reader表中

Python3 身份证号处理

表格中如何根据身份证号提取前6位的行政代码数字

js中验证身份证号码是否正确支持15位和18位身份证号