https://blog.csdn.net/qq_45738151/article/details/121308122

1. 安装导入必要的库

from faker import Faker
import random
import pymysql

2. Coding

  1. ```python
  2. # -*- coding: utf-8 -*-#
  3. # -------------------------------------------------------------------------------
  4. # Name: zh_name
  5. # Description:
  6. # Author: 牙叔
  7. # Date: 2021/11/13 17:48
  8. # -------------------------------------------------------------------------------
  9. from faker import Faker
  10. import random
  11. import pymysql
  12. province_id = [11, 12, 13, 14, 15, 21, 22, 23, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46,
  13. 50, 51, 52, 53, 54, 61, 62, 63, 65, 65, 81, 82, 83]
  14. phone_number = [139, 138, 137, 136, 135, 134, 159, 158, 15, 150, 151, 152, 188,
  15. 130, 131, 132, 156, 155, 133, 153, 189]
  16. # 随机生成出生日期
  17. def get_birthday():
  18. # 随机生成年月日
  19. year = random.randint(1960, 2000)
  20. month = random.randint(1, 12)
  21. # 判断每个月有多少天随机生成日
  22. if year % 4 == 0:
  23. if month in (1, 3, 5, 7, 8, 10, 12):
  24. day = random.randint(1, 31)
  25. elif month in (4, 6, 9, 11):
  26. day = random.randint(1, 30)
  27. else:
  28. day = random.randint(1, 29)
  29. else:
  30. if month in (1, 3, 5, 7, 8, 10, 12):
  31. day = random.randint(1, 31)
  32. elif month in (4, 6, 9, 11):
  33. day = random.randint(1, 30)
  34. else:
  35. day = random.randint(1, 28)
  36. # 小于10的月份前面加0
  37. if month < 10:
  38. month = '0' + str(month)
  39. if day < 10:
  40. day = '0' + str(day)
  41. birthday = str(year) + "-" + str(month) + "-" + str(day)
  42. return birthday
  43. # 随机生成手机号
  44. def get_tel():
  45. tel = ''
  46. tel += str(random.choice(phone_number))
  47. ran = ''
  48. for i in range(8):
  49. ran += str(random.randint(0, 9))
  50. tel += ran
  51. return tel
  52. get_sex = lambda: random.choice(['男', '女'])
  53. # 随机生成身份证号
  54. def get_idnum():
  55. id_num = ''
  56. # 随机选择地址码
  57. id_num += str(random.choice(province_id))
  58. # 随机生成4-6位地址码
  59. for i in range(4):
  60. ran_num = str(random.randint(0, 9))
  61. id_num += ran_num
  62. b = get_birthday()
  63. id_num += b
  64. # 生成15、16位顺序号
  65. num = ''
  66. for i in range(2):
  67. num += str(random.randint(0, 9))
  68. id_num += num
  69. # 通过性别判断生成第十七位数字 男单 女双
  70. s = get_sex()
  71. # print("性别:", s)
  72. if s == '男':
  73. # 生成奇数
  74. seventeen_num = random.randrange(1, 9, 2)
  75. else:
  76. seventeen_num = random.randrange(2, 9, 2)
  77. id_num += str(seventeen_num)
  78. eighteen_num = str(random.randint(1, 10))
  79. if eighteen_num == '10':
  80. eighteen_num = 'X'
  81. id_num += eighteen_num
  82. return id_num, s
  83. def get_DB_conn():
  84. mysql_conn = pymysql.connect(host='127.0.0.1', port=3306, user='admin', password='admin',
  85. db='persons', charset='utf8')
  86. return mysql_conn
  87. def main():
  88. mysql_conn = get_DB_conn()
  89. fake = Faker("zh_CN")
  90. for i in range(0, 10000):
  91. s = get_idnum()
  92. # tel = get_tel()
  93. # print(fake.name(), tel, get_birthday(), s[0], s[1])
  94. sql = "INSERT INTO persons (sname, stel, ssex, sprovince) VALUES ('{0}','{1}', '{2}','{3}')".format(fake.name(),
  95. get_tel(),
  96. s[1], s[0])
  97. print(sql)
  98. try:
  99. with mysql_conn.cursor() as cursor:
  100. cursor.execute(sql)
  101. mysql_conn.commit()
  102. except Exception as e:
  103. mysql_conn.rollback()
  104. mysql_conn.close()
  105. if __name__ == '__main__':
  106. main()
  107. print("=========================完成============================")