在測試的時候,有時候需要造客戶數據,可以用python快速生成姓名、手機號、身份證號、銀行卡號、郵箱
環境:python3.7、sublime text3中直接運行
import random
from datetime import date
from datetime import timedelta
class fourEl():
def create_phone(self):
prelist=["130", "131", "132", "133", "134",
"135", "136", "137", "138", "139",
"147", "150", "151", "152", "153",
"155", "156", "157", "158", "159",
"186", "187", "188", "189"]
iphone=random.choice(prelist)+"".join(random.choice("0123456789") for i in range(8))
return iphone
def getdistrictcode(self):
codelist = []
# 讀取地區碼
file = open('/Users/zouzhipeng/Downloads/districtcode.txt',
encoding='ISO-8859-1')
lines = file.readlines()
# 逐行讀取
for line in lines:
# 如果每行中去重後不為空,並且6位數字中最後兩位不為00,則添加到列表裡。(最後兩位為00時為省份或地級市代碼)
if line.lstrip().rstrip().strip() != '' and (line.lstrip().rstrip().strip())[:6][-2:] != '00':
codelist.append(line[:6])
return codelist
def create_idcard(self):
codelist = self.getdistrictcode()
id = codelist[random.randint(0, len(codelist))] # 地區項
id = id + str(random.randint(1950, 1998)) # 年份項
da = date.today() + timedelta(days=random.randint(1, 366)) # 月份和日期項
id = id + da.strftime('%m%d')
id = id + str(random.randint(100, 300)) # 順序號簡單處理
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(id)):
count = count + int(id[i]) * weight[i]
id = id + checkcode[str(count % 11)] # 算出校驗碼
return id
def create_name(self):
xing = ['趙', '錢', '孫', '李', '周', '吳', '鄭', '王', '馮', '陳',
'褚', '衛', '蔣', '沈', '韓', '楊', '朱', '秦', '尤', '許',
'何', '呂', '施', '張', '孔', '曹', '嚴', '華', '金', '魏',
'陶', '姜', '戚', '謝', '鄒', '喻', '柏', '水', '竇', '章',
'雲', '蘇', '潘', '葛', '奚', '範', '彭', '郎', '魯', '韋',
'昌', '馬', '苗', '鳳', '花', '方', '俞', '任', '袁', '柳',
'酆', '鮑', '史', '唐', '費', '廉', '岑', '薛', '雷', '賀',
'倪', '湯', '滕', '殷', '羅', '畢', '郝', '鄔', '安', '常',
'樂', '於', '時', '傅', '皮', '卞', '齊', '康', '伍', '餘',
'元', '卜', '顧', '孟', '平', '黃', '和', '穆', '蕭', '尹',
'姚', '邵', '堪', '汪', '祁', '毛', '禹', '狄', '米', '貝',
'明', '臧', '計', '伏', '成', '戴', '談', '宋', '茅', '龐',
'熊', '紀', '舒', '屈', '項', '祝', '董', '梁']
ming = ["子璇", "淼", "國棟", "夫子", "瑞堂", "甜", "敏", "尚", "國賢",
"賀祥", "晨濤","文昊","欣慧", "建政","慧嘉", "新建","偉洋",
"昊軒", "易軒", "益辰", "益帆", "鵬", "瑾春", "瑾昆", "春齊",
"偉", "雄霖", "浩晨", "熙涵", "溶溶", "冰楓", "欣欣", "宜豪",
"美欣", "淑慧", "文軒", "文傑", "欣源", "忠林", "翔", "欣汝",
"建林", "亦菲", "冰潔", "佳欣", "涵涵", "禹辰", "淳美", "澤惠",
"涵越", "潤麗", "淑華", "晶瑩", "凌晶", "林", "雨涵", "嘉怡",
"佳毅","澤遠", "欣怡","潤莎", "榕汕","雅晗", "雅涵","萌萌",
"子辰", "佳琪", "紫軒", "瑞辰", "昕蕊", "萌", "明遠", "欣宜",
"佳怡", "佳惠", "晨茜", "晨璐", "運昊", "汝鑫", "淑君", "晶瀅",
"佳鈺", "佳玉", "曉慶", "一鳴", "語晨", "添池", "添昊", "雨澤",
"清妍", "詩悅", "嘉樂", "晨涵", "天赫", "玥傲", "佳昊", "天昊",
"若萌"]
for i in range(1):
x = random.randint(0, len(xing))
m1 = random.randint(0, len(ming))
# m2 = random.randint(0, len(ming))
n = ('' + xing[x] + ming[m1])
return n
def create_bankAccount(self):
# 工行卡號開頭
prefix = "622202"
#prefix = "622609" # 招行卡
for i in range(13):
prefix = prefix + str(random.randint(0, 9))
return prefix
def createPhone(self):
# 第二個數字
second = [3, 4, 5, 7, 8][random.randint(0, 4)]
# 第三個數字
third = {3: random.randint(0, 9),
4: [5, 7, 9][random.randint(0, 2)],
5: [i for i in range(10) if i != 4][random.randint(0, 8)],
7: [i for i in range(10) if i not in [4, 9]][random.randint(0, 7)],
8: random.randint(0, 9), }[second]
# 最後8位數字
suffix = random.randint(9999999, 100000000)
# 拼接手機號
return "1{}{}{}".format(second, third, suffix)
if __name__ == '__main__':
a = fourEl()
b = a.create_name()
c = a.create_phone()
d = a.create_idcard()
e = a.create_bankAccount()
f = c+" q.com"
g = a.createPhone()
print("姓名:%s" % b)
print("手機號:%s" % c)
print("身份證號:%s" % d)
print("銀行卡號:%s" % e)
print("郵箱:%s" % f)
運行結果:
閱讀更多 恆力信心 的文章