一、概述
本次案例使用的两份数据格式分别为csv和json,根据面向对象思想,可分别编写用于读取两类文件的类;并编写一个数据类,用来存储读取到的信息。
最后在程序主文件中,先建立数据库链接,通过链接对象执行SQL语句。新建两个空表用于存储这些数据--->插入数据,最后关闭链接即可。(新建表操作可以在MySQL图形化界面中进行,更加高效快捷,此处是为练习才使用python间接完成)
二、代码
"""
数据封装的类
"""
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}"
"""
文件读取类
"""
import json
from data_define import Record
#定义一个抽象类做顶层设计,确定需要实现的功能
class FileReader:
def read_data(self) -> list[Record]:
""