欢迎光临散文网 会员登陆 & 注册

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

2023-06-18 19:26 作者:JayJu  | 我要投稿

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

黑马程序员python教程,8天python从入门到精通,学python看这套就的评论 (共 条)

分享到微博请遵守国家法律