Python基础——数据分析考核

Python基础——数据分析考核,第1张

数据生成文件:

类名:【GetRandomInfo.py】

import random


def getClassName():
    randomClass = ("天字一号", "地字一号", "玄字一号", "黄字一号", "东宫正品")
    return randomClass[int(random.random() * len(randomClass))]


def getName():
    """随机姓名"""
    randomNames = (
        "华", "韦", "覃", "琴", "湘", "", "钱", "彭", "邓" "张", "王", "里", "赵", "赵", "李", "春", "梦", "星", "二", "宿", "管", "土", "生",
        "风", "胡")
    return str.format("{0}{1}{2}", randomNames[int(random.random() * len(randomNames))],
                      randomNames[int(random.random() * len(randomNames))],
                      randomNames[int(random.random() * len(randomNames))])


def getSex():
    """随机性别"""
    sex = ("男", "男", "女")
    return sex[int(random.random() * 3)]


def getSubJectName():
    """随机科目"""
    randomClass = ("java", "python", "C#", "php", "go")
    return randomClass[int(random.random() * len(randomClass))]


def getResults():
    """随机成绩"""
    return int(random.random() * 41)+60


def GetRandomInfo():
    """获取500条随机信息"""
    data = []
    for index in range(1, 501):
        strInfo = str.format("{0} {1} {2} {3} {4} {5}", index, getClassName(), getName(), getSex(), getSubJectName(),
                             getResults())
        data.append(strInfo.split(","))
    return data
写入【csv】测试信息:

类名:【WriteCsv.py】

import csv
import GetRandomInfo

# 文件获取
file = open('info.csv', 'w+', encoding="utf-8")
# 写入 *** 作
writer = csv.writer(file)

# 写入多行记录
writer.writerows(GetRandomInfo.GetRandomInfo())

# 刷新文件
file.flush()

# 关闭文件流
file.close()
读取【csv】测试数据:

类名:【ReadCsv.py】

import csv

# 文件获取
file = open('info.csv', 'r', encoding="utf-8")
# 内容读取
list1 = csv.reader(file)
# 计数器
countBoy = 0
countGirl = 0
CountJava = 0
SumBoyResults = 0
SumGirlResults = 0
# 信息遍历
for line in list1:
    if len(line) != 0:
        for row in line:
            arr = row.split(" ")
            if arr[3] == "男":
                countBoy += 1
                SumBoyResults += int(arr[5])
            else:
                countGirl += 1
                SumGirlResults += int(arr[5])
            if arr[4] == "java":
                CountJava += 1

print("男孩数量:", countBoy)
print("女孩数量:", countGirl)
print("男孩平均成绩:", "%.2f" % (SumBoyResults / countBoy))
print("女孩平均成绩:", "%.2f" % (SumGirlResults / countGirl))
print("java班共计:", CountJava, "人")
简单数据分析: 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/570863.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存