高考结束了,教你用Python来伪造900万条成绩数据!

《高考结束了,教你用Python来伪造900万条成绩数据!》 高考结束了,教你用Python来伪造900万条成绩数据!
《高考结束了,教你用Python来伪造900万条成绩数据!》 PythonEco

生成文件内容部分截图:

《高考结束了,教你用Python来伪造900万条成绩数据!》 PythonEco

测试数据生成代码:

from faker import Faker
import random
import numpy as np
import time
import sys

def get_random_name():  # 生成随机姓名
    fake1 = Faker()
    return fake1.name()

def get_random_id():    # 生成10位准考证号
    id_list = []
    for i in range(10):
        random_num = random.randint(0, 9)
        id_list.append(random_num)
    num_list = ''.join(str(i) for i in id_list)  # 遍历list的元素,把他转化成字符串
    return num_list

def get_random_score():  # 生成随机成绩
    random_score = np.random.normal(96, 6)  # 正态分布
    return int(random_score)

def get_random_score2():  # 生成随机综合成绩
    random_score2 = np.random.normal(250, 8)  # 正态分布
    return int(random_score2)

def get_fake_data():
    temp = sys.stdout      # 记录当前输出指向,默认是consle
    for i in range(60):    # 这里以生成60条数据为测试
        id_num = get_random_id()
        name = get_random_name()
        Chinese = get_random_score()
        Math = get_random_score()
        English = get_random_score()
        Zonghe = get_random_score2()
        Total = Chinese+Math+English+Zonghe
        with open("D:\新建文件夹\FakeData.txt", "a")as f:             # 两次输出重定向
            sys.stdout = f       # 输出指向txt文件
            print(id_num, name, Chinese, Math, English, Zonghe, Total)
            sys.stdout = temp    # 输出重定向回consle

if __name__ == '__main__':
    start = time.clock()
    get_fake_data()
    end = time.clock()
    print('Running time:%s Seconds' % (end-start))

好了,本次实验目的主要是学习使用Python文件读写和掌握Python字符处理。

See U next time!

    原文作者:Sheldon_86fd
    原文地址: https://www.jianshu.com/p/4139472f2fbf
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞