from random import randint
def id_creat():
std_id = randint(1,1000000)
if std_id < 100000:
std_id = (6 - len(str(std_id))) * '0' + str(std_id)
return std_id
def modify_file(old_file, new_file):
id_list = []
with open(old_file, 'r') as old:
l_old = old.read()
a = list(map(lambda x: x.capitalize(),l_old.split('\n'))) # 转list并修改首字母为大写
for i in range(len(a)):
id_list.append(id_creat())
d_n = dict.fromkeys(a, )
for x in d_n:
d_n[x] = id_creat()
with open(new_file, 'a') as new:
for key, val in d_n.items():
new.write(f'{key}, id={val}\n')
modify_file('old.txt', 'names.txt')
old.txt
andrew johnson
carl brown
james wellington
hannah metcalfe
carl faith dyer
maria arnold
colin hudson
edward martin
irene sharp
william ross
elizabeth miller
nicola terry
zoe karen walker
rachael short
alison gibson
生成的names.txt
Andrew johnson, id=660423
Carl brown, id=736681
James wellington, id=474974
Hannah metcalfe, id=077356
Carl faith dyer, id=874478
Maria arnold, id=798678
Colin hudson, id=795771
Edward martin, id=631234
Irene sharp, id=308567
William ross, id=819149
Elizabeth miller, id=815311
Nicola terry, id=128637
Zoe karen walker, id=479428
Rachael short, id=124619
Alison gibson, id=650236