Generate Random Student Records in Python: TXT & CSV File Tutorial
This article walks through a Python exercise that creates random student names, IDs, and scores, then saves the data into both TXT and CSV files using the random module, lists, dictionaries, and file I/O, complete with full code examples and output screenshots.
Introduction
The author, a Python enthusiast, shares a practical exercise prompted by a fan’s question about generating student records with random data.
Problem Description
The task is to generate random Chinese names, student numbers, and scores, then store each record as a dictionary in a file. The problem tests knowledge of the random library, lists, dictionaries, and file operations.
Approach
Two lists—one for surnames and one for given names—are prepared. A random surname and a random given name are concatenated to form a full name. A sequential student number is generated, and a random score between 0 and 100 is assigned. Each record is stored in a dictionary and written to a file.
Solution
1) TXT File Storage
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName :test.py
# @Time :2021/9/19 21:34
# @Author :Cat
import random
xing = ['赵','钱','孙','李','周','吴','郑','王','冯','陈','褚','卫','蒋','沈','韩','杨','朱','秦','尤','许','何','吕','施','张','孔','曹','严','华','金','魏','陶','姜','戚','谢','邹','喻','柏','水','窦','章','云','苏','潘','葛','奚','范','彭','郎','鲁','韦','昌','马','苗','凤','花','方','俞','任','袁','柳','酆','鲍','史','唐','费','廉','岑','薛','雷','贺','倪','汤','滕','殷','罗','毕','郝','邬','安','常','乐','于','时','傅','皮','卞','齐','康','伍','余','元','卜','顾','孟','平','黄','和','穆','萧','尹','姚','邵','湛','汪','祁','毛','禹','狄','米','贝','明','臧','计','伏','成','戴','谈','宋','茅','庞','熊','纪','舒','屈','项','祝','董','梁']
ming = ['凡','佳','勤','珍','贞','鸿','焕','风','朗','浩','亮','政','谦','振','壮','伟','刚','勇','毅','俊','峰','强','军','平','保','东','文','辉','力','固','之','段','殿','泰','利','清','飞','彬','富','顺','信','子','杰','涛','昌','成','康','星','翰','诚','博','先','敬','若','鸣','朋','斌','梁','栋','维','启','克','伦','翔','旭','鹏','泽','朗','伯','彪','晋','晟','诚','先','敬','震','振','壮','会','思','群','豪','心','邦','承','乐','宏','言','旲','旻','昊','光','天','达','安','岩','中','茂','进','林','有','坚','和','彪','博','泰','盛','振','挺','掣','明','永','健','世','广','志','义','兴','良','海','山','仁','波','宁','行','时','志','忠','思','绍','功','松','善','厚','庆','磊','民','友','裕','河','哲','江','超','炎','德','彰','征','律','晨','辰','士','以','建','家','致','煜','煊','炎','波','宁','贵','福','生','龙','元','全','国','胜','学','祥','才','发','武','新','利','清','飞','彬','富','顺','信','子','杰','涛','昌','成','康','星','光','天','达','安','岩','中','茂','进','林','有','坚','和']
with open('students.txt', 'a', encoding='utf-8') as f:
for i in range(9):
item = {}
item['姓名'] = random.choice(xing) + random.choice(ming)
item['学号'] = str(2020010001 + i)
item['成绩'] = random.randint(0, 100)
f.write(str(item))
f.write('
')Running the script produces a TXT file with random records, as shown:
2) CSV File Storage
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName :test_csv.py
# @Time :2021/9/26 21:01
# @Author :Cat
import random
import csv
headers = ['姓名', '学号', '成绩']
xing = ['赵','钱','孙','李','周','吴','郑','王','冯','陈','褚','卫','蒋','沈','韩','杨','朱','秦','尤','许','何','吕','施','张','孔','曹','严','华','金','魏','陶','姜','戚','谢','邹','喻','柏','水','窦','章','云','苏','潘','葛','奚','范','彭','郎','鲁','韦','昌','马','苗','凤','花','方','俞','任','袁','柳','酆','鲍','史','唐','费','廉','岑','薛','雷','贺','倪','汤','滕','殷','罗','毕','郝','邬','安','常','乐','于','时','傅','皮','卞','齐','康','伍','余','元','卜','顾','孟','平','黄','和','穆','萧','尹','姚','邵','湛','汪','祁','毛','禹','狄','米','贝','明','臧','计','伏','成','戴','谈','宋','茅','庞','熊','纪','舒','屈','项','祝','董','梁']
ming = ['凡','佳','勤','珍','贞','鸿','焕','风','朗','浩','亮','政','谦','振','壮','伟','刚','勇','毅','俊','峰','强','军','平','保','东','文','辉','力','固','之','段','殿','泰','利','清','飞','彬','富','顺','信','子','杰','涛','昌','成','康','星','翰','诚','博','先','敬','若','鸣','朋','斌','梁','栋','维','启','克','伦','翔','旭','鹏','泽','朗','伯','彪','晋','晟','诚','先','敬','震','振','壮','会','思','群','豪','心','邦','承','乐','宏','言','旲','旻','昊','光','天','达','安','岩','中','茂','进','林','有','坚','和','彪','博','泰','盛','振','挺','掣','明','永','健','世','广','志','义','兴','良','海','山','仁','波','宁','行','时','志','忠','思','绍','功','松','善','厚','庆','磊','民','友','裕','河','哲','江','超','炎','德','彰','征','律','晨','辰','士','以','建','家','致','煜','煊','炎','波','宁','贵','福','生','龙','元','全','国','胜','学','祥','才','发','武','新','利','清','飞','彬','富','顺','信','子','杰','涛','昌','成','康','星','光','天','达','安','岩','中','茂','进','林','有','坚','和']
with open('students.csv', 'a', encoding='utf-8', newline='') as f:
csv_writer = csv.DictWriter(f, headers)
csv_writer.writeheader()
for i in range(9):
item = {}
item['姓名'] = random.choice(xing) + random.choice(ming)
item['学号'] = str(2020010001 + i)
item['成绩'] = random.randint(0, 100)
csv_writer.writerow(item)Running the script produces a CSV file with random records, as shown:
Conclusion
The exercise demonstrates how to use Python’s random module, list handling, dictionary creation, and file I/O to generate synthetic student data and store it in both plain‑text and CSV formats. Readers are encouraged to try the code themselves and adapt it for other data‑generation tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
