说明:通过循环和随机生成随机的数据元组,再通过python的csv包将随机数据元组写入csv文件中,再通过Management Studio的数据导入功能将数据导入到基本表中。
代码借鉴其他博主文章修改而得
import requests
import sys
import re
import csv
import random
data = [0]*5
'''
往csv文件中写入数据
'''
def writerCsv():
fileHeader = ["SNO", "SNAME","SSEX","SAGE","SDEPAT"]
csvFile = open("D:/python/pythonProject2/Student.csv", "w",newline='',encoding='utf8')
writer = csv.writer(csvFile)
base_name = 'abcdefghijklmnopqrstuvwxyz'
base_sex = ['boy', 'girl']
base_depat = ['CS', 'IS', 'IOT', 'NE', 'CE', 'AM', 'ME', 'FI', 'AI']
line = 1
for i in range(0,200000):
if line==1:
writer.writerow(fileHeader)
data[0] = line #SNO
data[1] = ''.join(random.sample(base_name, random.randint(7, 9))) #SNAME
data[2] = random.choice(base_sex) #SEX
data[3] = random.randint(17,24) #SAGE
data[4] = random.choice(base_depat) #SDEPAT
writer.writerow(data)
print(line)
line+=1
csvFile.close()
writerCsv()
结果: