数据分析案例
data_define.py
# 数据定义的类 class Record: def __init__(self, date, order_id, money, province): self.date = date self.order_id = order_id self.money = money self.province = province def __str__(self): return f"{self.date}, {self.order_id}, {self.money}, {self.province}"
file_define.py
# 因为读取文件的格式不同,所以采取抽象类 # 和文件相关的类定义 # 先定义一个抽象类用于做顶层设计 import json from 数据分析案例.data_define import Record class FileReader: def read_data(self) -> list[Record]: """将读到的数据转换为Record对象""" pass class TextFileReader(FileReader): def __init__(self, path): self.path = path # 定义文件变量的路径 # 复写 def read_data(self) ->list[Record]: f = open(self.path, "r", encoding="utf-8") record_list: list[Record] = [] for line in f.readlines(): line = line.strip() # 消去回车换行符 data_list = line.split(",") record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3]) record_list.append(record) f.close() return record_list class JsonFileReader(FileReader): def __init__(self, path): self.path = path def read_data(self) -> list[Record]: f = open(self.path, "r", encoding="utf-8") record_list: list[Record] = [] for line in f.readlines(): data_dict = json.loads(line) record = Record(data_dict["date"], data_dict["order_id"], data_dict["money"], data_dict["province"]) record_list.append(record) f.close() return record_list if __name__ == '__main__': text_file_reader = TextFileReader("D:/BaiduNetdiskDownload/第13章资料/2011年1月销售数据.txt") json_file_reader = JsonFileReader("D:/BaiduNetdiskDownload/第13章资料/2011年2月销售数据JSON.txt") list1 = text_file_reader.read_data() list2 = json_file_reader.read_data() for l in list1: print(l) for l in list2: print(l)
main.py
from file_define import FileReader, TextFileReader, JsonFileReader from data_define import Record text_file_reader = TextFileReader("D:/BaiduNetdiskDownload/第13章资料/2011年1月销售数据.txt") json_file_reader = JsonFileReader("D:/BaiduNetdiskDownload/第13章资料/2011年2月销售数据JSON.txt") # 获取月份数据 jan_data: list[Record] = text_file_reader.read_data() feb_data: list[Record] = json_file_reader.read_data() # 将两个月份的数据合成一个list all_data: list[Record] = jan_data + feb_data # 开始数据计算 data_dict = {} for record in all_data: if record.date in data_dict.keys(): """和老记录做累加即可""" data_dict[record.date] += record.money else: data_dict[record.date] = record.money print(data_dict) # 可视化开发 from pyecharts.charts import Bar from pyecharts.options import * from pyecharts.globals import ThemeType bar = Bar(init_opts=InitOpts(theme=ThemeType.LIGHT)) bar.add_xaxis(list(data_dict.keys())) bar.add_yaxis("销售额", list(data_dict.values()), label_opts=LabelOpts(is_show=False)) bar.set_global_opts( title_opts=TitleOpts(title="每日销售额") ) bar.render("每日销售额柱状图.html")
结果图: