數(shù)據(jù)準備:兩份數(shù)據(jù),一份是是字符串的形式,一份是json格式,之后對數(shù)據(jù)處理后,需要合并為一份的數(shù)據(jù),進而進行圖表的開發(fā)
?
?
1?設(shè)計一個類,完成對數(shù)據(jù)的封裝
"""
數(shù)據(jù)定義的類
"""
class Record:
date = None # 訂單日期
order_id = None # 訂單id
money = None # 訂單金額
province = None # 銷售地區(qū)
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"date: {self.date}, order_id: {self.order_id}, money: {self.money}, province: {self.province}"
2?設(shè)計一個抽象類,定義文件讀取的相關(guān)功能,并使用子類實現(xiàn)具體的功能
"""
和文件相關(guān)的類定義
"""
from data_defined import Record
import json
# 先定義一個抽象類來做頂層設(shè)計,確定有哪些功能需要實現(xiàn)
class FileReader:
def read_data(self):
"""讀取文件的數(shù)據(jù),讀到的每一條數(shù)據(jù)都轉(zhuǎn)換為Record對象,將它們都封裝到list內(nèi),返回即可"""
pass
# 子類1
class TextFileReader(FileReader):
def __init__(self, path):
self.path = path # 定義成員變量,記錄文件的路徑
# 復寫父類的方法(實現(xiàn)抽象方法)
def read_data(self):
"""讀取文本文件的數(shù)據(jù)"""
fr1 = open(self.path, 'r', encoding="UTF-8")
lines = fr1.readlines()
fr1.close()
record_list: list[Record] = []
for line in lines:
line = line.strip("\n") # 去掉換行符
data_ls = line.split(",")
# print(data_ls)
record = Record(data_ls[0], data_ls[1], int(data_ls[2]), data_ls[3])
record_list.append(record)
return record_list
# 子類2
class JsonFileReader(FileReader):
def __init__(self, path):
self.path = path # 定義成員變量,記錄文件的路徑
# 復寫父類的方法(實現(xiàn)抽象方法)
def read_data(self):
"""讀取json文件的數(shù)據(jù)"""
fr2 = open(self.path, "r", encoding="UTF-8")
lines = fr2.readlines()
fr2.close()
record_list: list[Record] = []
for line in lines:
data_dict = json.loads(line)
record = Record(data_dict["date"], data_dict["order_id"], int(data_dict["money"]), data_dict["province"])
record_list.append(record)
return record_list
3 數(shù)據(jù)處理
from file_defined import FileReader, TextFileReader, JsonFileReader
from data_defined import Record
text_file_reader = TextFileReader("D:/PyCharm_projects/python_study_projects/text/2011年1月銷售數(shù)據(jù).txt")
data1 = text_file_reader.read_data()
json_file_reader = JsonFileReader("D:/PyCharm_projects/python_study_projects/text/2011年2月銷售數(shù)據(jù)JSON.txt")
data2 = json_file_reader.read_data()
# print(type(data2)) # list
print(data2)
# 將2個月份的數(shù)據(jù)合并為1個list
all_data = data1 + data2
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)
?文章來源:http://www.zghlxwxcb.cn/news/detail-797174.html
4 可視化圖表開發(fā)
from pyecharts.charts import Bar
from pyecharts.options import *
from pyecharts.globals import ThemeType
# 可視化圖表開發(fā)
bar = Bar(init_opts=InitOpts(theme=ThemeType.LIGHT)) # 設(shè)置主題
bar.add_xaxis(list(data_dict.keys())) # 添加 x 軸的數(shù)據(jù)
bar.add_yaxis("銷售額", list(data_dict.values()), label_opts=LabelOpts(is_show=False)) # 添加 y 軸的數(shù)據(jù), is_show=False表示不展示數(shù)據(jù)
bar.set_global_opts(
title_opts=TitleOpts(title="2011年1月-2月銷售數(shù)據(jù)", pos_left="center", pos_top="5%")
)
bar.render("D:/PyCharm_projects/python_study_projects/modules/bar_sale_chart.html")
文章來源地址http://www.zghlxwxcb.cn/news/detail-797174.html
到了這里,關(guān)于Pyhton基礎(chǔ)知識:整理18 -> 基于面向?qū)ο蟮闹R完成數(shù)據(jù)分析的案例開發(fā)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!