Python外部数据源文件处理

作业问题

  1. python 写代码的命名问题
    https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules/
    文件名、类名、等等不要用中文
  2. 类、类对象、实例对象
class C:
    # 类的属性
    cc = 10

print("类的静态属性",C.cc)
a = C()
b = C()
print("实例对象a",a.cc)
print("实例对象b",b.cc)
b.cc = 20
print("第二次,类的静态属性",C.cc)
print("第二次,实例对象a",a.cc)
print("第二次,实例对象b",b.cc)
C.cc = 30
print("第3次,类的静态属性",C.cc)
print("第3次,实例对象a",a.cc)
print("第3次,实例对象b",b.cc)
class C:
    # 类的属性
    cc = 10
    def method_c(self):
        return "a"

#使用__dict__查看对象的属性
print(C.__dict__)
a = C()
print("第一次打印实例对象属性",a.__dict__)
a.cc = 20
print("第2次打印实例对象属性",a.__dict__)

json

python json 的官方文档:

import json

class PracticeJson:
    def __init__(self):
        self.data = {"z":1, "a":2, "b":3}

    def practice_dumps(self):
        # print("dumps之前")
        # print(type(self.data))
        # print("dumps之后")
        # print(type(json.dumps(self.data)))
        print(json.dumps(self.data, sort_keys=True , indent=4))
        # json.dump(self.data, sort_keys=True)

    def practice_dump(self):
        with open("./demo1.json", "w") as fs:
            json.dump(self.data, fp=fs)

    def practice_loads(self):

        json_data = json.dumps(self.data)
        print("loads之前")
        print(type(json_data))
        print("loads之后")
        print(type(json.loads(json_data)))

    def practice_load(self):
        print(json.load(open("demo.json")))

excel

好用的第三方库集合:


openpyxl库:
https://openpyxl.readthedocs.io/en/stable/

from openpyxl import Workbook, load_workbook


class PracticeExcel:
    def create_data(self):
        """
        创建excel数据
        :return:
        """
        wb = Workbook()
        ws1 = wb.active
        ws1.title = "create"
        ws1["A1"] = "身高"
        ws1["B1"] = "体重"
        height = [180, 160, 170, 155]
        weight = [60, 55, 60, 80]
        data_dict = {180:61, 160:51 , 170:61, 155:81}
        data_keys = [i for i in data_dict.keys()]

        for i in range(len(height)):
            ws1.cell(row=i+2, column=1).value = data_keys[i]
            ws1.cell(row=i+2, column=2).value = data_dict[data_keys[i]]
        # #读取列表
        # for i in range(len(height)):
        #     ws1.cell()
        #     ws1.cell(row=i+2, column=1).value = height[i]
        #     ws1.cell(row=i+2, column=2).value = weight[i]
        wb.save("data.xlsx")

    def get_data(self):
        ld = load_workbook(filename="data.xlsx")
        sheet = ld["create"]
        for i in range(5):
            print(sheet.cell(row=i + 1, column=1).value)

    def health_data(self):
        ld = load_workbook(filename="data.xlsx")
        sheet = ld["create"]
        sheet["C1"] = "备注"

        for i in range(4):
            height = sheet.cell(row=i + 2, column=1).value
            weight = sheet.cell(row=i + 2, column=2).value

            health_weight = (height - 70) * 0.6
            if weight == health_weight:
                print("这是健康的体重",weight)
                sheet.cell(row=i + 2, column=3).value = "健康体重"
        ld.save(filename="data.xlsx")


pe = PracticeExcel()
pe.health_data()

关闭