黑马程序员python教程,8天python从入门到精通,学python看这套就

P124
import json
class dataInfo:
def __init__(self, data: str, id: str, money: str, province: str) -> None:
self.date: str = data
self.id: str = id
try:
self.money: int = int(money)
except Exception:
print("Error:Money isn't number!")
self.province: str = province
class data:
path: str = None
__list:list[dataInfo] = []
def __init__(self, path: str) -> None:
self.path = path
def ReadCsv(self) -> None:
'''Csv格式的数据读取'''
with open(self.path, "r", encoding="utf-8") as f:
for x in f.readlines():
# 单行数据整理,去除\n
temp = x.strip()
# 数据拆分
temp = temp.split(",")
self.__list.append(dataInfo(temp[0], temp[1], temp[2], temp[3])
)
def ReadJson(self) -> None:
'''Json格式的数据读取'''
with open(self.path, "r", encoding="utf-8") as f:
for x in json.loads(f.read()):
self.__list.append(
dataInfo(x['date'], x['order id'], x['money'], x['province'])
)
def Get(self) -> list[dataInfo]:
'''获得数据列表'''
return self.__list