前段时间做了一个数据库的课设,其中姓名的初始数据引起了我的思考,于是下面的用于随机生成姓名的代码便诞生了:
import random
import os
class CreateName:
"""生成名字"""
def __init__(self, numbers=100):
# 用来存姓氏
self.first_names = []
# 用来存名,包含双字
self.last_names = []
# 标志位
self.sign = True
# 用来存最终的名字
self.names = []
# 生成名字数
self.numbers = numbers
def create_first_name(self):
"""将百家姓做成一个列表"""
# 拼接文件路径
with open(os.path.dirname(
os.path.realpath(__file__)) + '/the_first_name', 'r',
encoding='UTF-8') as f:
# 获取百家姓
the_hundred_surnames = f.readlines()
# 先处理单姓,第51行之前的都是单性
for the_hundred_surname in the_hundred_surnames[:51]:
for the_surname in the_hundred_surname[