Python基础——数据分析考核(基础版本)

举报
红目香薰 发表于 2022/04/20 20:10:37 2022/04/20
【摘要】 ​ 数据生成文件:类名:【GetRandomInfo.py】import randomdef getClassName(): randomClass = ("天字一号", "地字一号", "玄字一号", "黄字一号", "东宫正品") return randomClass[int(random.random() * len(randomClass))]def getName():...

 数据生成文件:

类名:【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, "人")

简单数据分析: 


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。