又开始的python-day10-20200821-文件操作相关内置函数-拷贝-读取-写入

    科技2024-07-07  64

    # 作业1 # 设计两个类 # 一个点累 属性 x y 坐标 # 一个矩形类 属性有左上角 和 右下角坐标 # 方法 1.计算矩形面积 2.判断点是否在矩形内 # 实例化一个点对象 一个正方形对象 输出矩形的面积 输出点是否在矩形内 class Point(object): def __init__(self,x:int,y:int): # 需要2个int 类型的参数 self.x = x self.y = y class Rectangle(object): def __init__(self,top_left:Point,bottom_right:Point): self.top_left = top_left self.bottom_right = bottom_right def get_area(self): length = abs(self.bottom_right.x - self.top_left.x) width = abs(self.top_left.y - self.bottom_right.y) return length * width def is_inside(self,point): if self.bottom_right.x >= point.x >= self.top_left.x and self.top_left.y >= point.y >= self.bottom_right.y: return True else: return False p1 = Point(4,20) p2 = Point(30,8) r = Rectangle(p1,p2) print(r.get_area()) p = Point(10,13) print(r.is_inside(p)) pp = Point(4,19) print(r.is_inside(pp)) # 建立一个汽车类Auto # 包括轮胎个数 汽车颜色 车身重量 速度等属性 并通过不同的构造方法创建实例 # 至少要求 汽车可以加速减速停车 # 在定义一个小汽车类CarAuto 继承Auto 并添加空调 CD 属性 # 并且实现方法覆盖加速 减速的方法 class Auto(object): def __init__(self,color,weight,speed=0,wheel_count=4): self.color = color self.weight = weight self.speed = speed self.wheel_count = wheel_count def chamge_speed(self,x): if x == 0: self.speed = 0 return self.speed += x # if self.speed <= 0 and x < 0: # 车速为0 # return # 表示函数结束 class CarAuto(Auto): def __init__(self,color,weight,ac,navigator,speed=0,wheel_count=4): super(CarAuto,self).__init__(color,weight,speed,wheel_count) self.navigator = navigator self.ac = ac car = Auto('白色',1.6) car.chamge_speed(-10) print(car.speed) car.chamge_speed(100) print(car.speed) car.chamge_speed(0) print(car.speed) car = CarAuto('白色',1.6,'美的','Android') # open 内置函数 打开一个文件 # file:文件的路径 # mode: 打开文件的模式 r:只读 w:写入 b:二进制 t:文本形式打开 # mode: 默认使用rt # encoding: 用来指定文件的编码格式 win 系统 默认是gbk # file = open('xxx.txt') # 默认以rt打开 文件不存在报错 # file = open('xxx.txt','w',encoding='utf8') # 创建一个新的文件 # file.write('你好') # file.close() file = open('xxx.txt',encoding='utf8') print(file.read()) # 全部读出来 如果文件很大 负载会很大 尽量采取分开读 print(file.readline()) # 只读取一行数据 file.readlines() # 读取所有行的 数据 放到列表里 file.close() #x = file.read(10) # 指的是读取的长度 #print(x) # 优化:没有绝对的优化 除非加硬件 # import os # # 文件拷贝功能 # file_name = input('请输入一个文件路径:') # if os.path.isfile(file_name): # 判断文件是否存在 # # 打开旧文件 # old_file = open(file_name,'rb') # 以二进制的形式读 # # old_file = open(file_name,encoding='utf8') # # name = file_name.rpartition('.') # sss . txt # # new_file_name = names[0] + '.bak.' + names[2] # # new_file_name= 'test.txt' # # 生成新的文件 # name = os.path.splitext(file_name) # sss .txt # new_file_name = names[0] + '.bak.' + names[1] # new_file = open(new_file_name,'wb') # 以二进制的形式写 # # new_file = open(new_file_name,'w',encoding='utf8') # # new_file.write(old_file.read()) # while Trun: # content = old_file.read(1024) # 读写分成一段一段的 防止文件过大 都挤到内存里 # new_file.write(content) # if not content: # break # # 时间多了些 但效率好点 # new_file.close() # old_file.close() # else: # print('输入的文件不存在') # csv 的文件读写 # csv 文件 逗号分隔值 # 用纯文本的形式存储表格数据 # 写入 import csv # file = open('test.csv','w',encoding='utf8',newline='') # 打开一个文件 newline 就不会多出来空行了 # w = csv.writer(file) # # w.writerow(['name','age','score','city']) # # w.writerow(['zhangsan','22','99','北京']) # # w.writerow(['lisi','23','100','深圳']) # # 一行一行 和 多行添加 # w.writerows( # [ # ['name','age','score','city'], # ['zhangsan','22','99','北京'], # ['lisi','23','100','深圳'], # ['zss','22','100','深圳'], # ] # ) # file.close() # 读取 file = open('test.csv','r',encoding='utf8',newline='') r = csv.reader(file) for data in r: print(data) file.close() ## 将数据写入到内存涉及到 StringIO BytesIO from io import StringIO,BytesIO s_io = StringIO() s_io.write('hello') # 把数据写入到了内存缓存起来 s_io.write('good') print(s_io.getvalue()) s_io.close() s = '' s += 'hello' s += 'world' s += 'python' print(s) # file 需要一个文件流对象 s_io = StringIO() print('hello',file=s_io) print('helloo',file=s_io) print('hellooo',file=s_io) print(s_io.getvalue()) s_io.close() # 二进制 b_io = BytesIO() b_io.write('你好'.encode('utf8')) print(b_io.getvalue()) print(b_io.getvalue().decode('utf8')) b_io.close() # sys模块使用 import sys # sys.stdin 接收用户的输入 # sys.stdout 标准输出 # sys.stderr 错误输出 # s_in = sys.stdin # while True: # content = s_in.readline().rstrip('\n') # hello\n == hello \n == '' # if content == '': # break # print(content) # sys.stdout = open('stdout.txt','w',encoding='utf8') # print('2020') # print('good') # sys.stdout.close() # sys.stderr = open('stderr.txt','w',encoding='utf8') # print(1 / 0) # sys.stderr.close() ## 序列化 和 反序列化 # 序列化:将数据从内存持久化保存到本地的过程 # 反序列化:将数据从本地加载到内存的过程 # write 只能写入字符串或者二进制 # 字典 列表 数字等都不能直接写入到文件 # 将数据转换为字符串 repr/str 使用json模块 # json 本质就是字符串 区别在于json里要用双引号表示字符串 # 将数据转换为二进制 使用pickle模块 import json names = ['zss','lisi','jack','tony','shy'] x = json.dumps(names) # dumps 将数据转换为json字符串 print(x) # json 数据持久化的2个方法 dumps dump file = open('names.txt','w',encoding='utf8') # names = ['zss','lisi','jack','tony'] # file.write(str(names)) # file.write(x) json.dump(names,file) # dump 将数据转换为json字符串的同时写入到指定文件 file.close() # json 反序列化 2个方法 # loads 将json 字符串加载为python里的数据 # load 读取文件 把读取的内容加载为python里的数据 x = '{"name":"lisi", "age":"18"}' p = json.loads(x) print(p) x = open('names.txt','r',encoding='utf8') p = json.load(x) print(p) x.close() # json python 转换有对应类型 # # pickle 模块的使用 # python 里存入数据只支持 字符串 二进制 # json 将python里的数据(str/list/tuple/dict/int/float/bool/None)等转换为对应的json类型 # pickle 将python里任意对象转换为二进制 import pickle # pickle 下的方法 # 序列化 dumps 将python数据转换为二进制 # # dump 将python数据转换为二进制的同时写入到指定文件 # 反序列化 loads 将二进制加载为python里的数据 # # load 读取文件 把文件的二进制内容加载为python里的数据 names = ['zss', 'lisi', 'jack', 'tony', 'shy'] b_names = pickle.dumps(names) print(b_names) # 二进制的了 # 一种方法 file = open('names.txt','wb') file.write(b_names) # 写入的是二进制 file.close() file1 = open('names.txt','rb') x = file1.read() y = pickle.loads(x) print(y) file1.close() # 第二种方法 file2 = open('names.txt','wb') pickle.dump(names,file2) file2.close() # file3 = open('names.txt','rb') pickle.load(file3) file3.close() # 应用 class Dog(object): def __init__(self,name,color): self.name = name self.color = color def eat(self): print(self.name + '在吃东西') d = Dog('大黄','白色') # 保存到文件里 原封不动的保存 pickle.dump(d,open('dog.txt','wb')) # 从文件加载出来 加载出来还可以直接调用方法 dd = pickle.load(open('dog.txt','rb')) dd.eat() print(dd.name,dd.color) # json 和 pickle 区别 # pickle 用来将数据原封不动转换为二进制 但是这个二进制 只能在python识别 # json 只能保存一部分信息 作用是用来在不同的平台传递数据 json 存储的数据基本都是基本的数据类型 # json 将对象转换为字符串 不管在哪个操作系统 编程语言 字符串都是可以识别的 但也并不是所有的都可以 有个对应关系 只有对应的才可以 不然报错 # 前端页面数据是对象 json字符串 info = '{"name":"zss", "age":18, "height":1.75,"pass": true,"hobbies": ["sing","dance","basketball","rap"]}' # 后端服务器接收之后要转换 person = json.loads(info) print(person) # {'name': 'zss', 'age': 18, 'height': 1.75, 'pass': True, 'hobbies': ['sing', 'dance', 'basketball', 'rap']} print(person['name']) # 把用户信息写入到数据库服务器 返回成功的信息 result = {'success':True} x = json.dumps(result) # 将数据转换成json字符串 print(x) # 前端页面接收到之后 再进行对应转换 # result = JSON.parse(x) # 接口文档: # 全栈工程师 # 服务器工程师更容易成长为全栈工程师 # 如果是一个自定义对象 默认无法转换为json 字符串 需要手动指定JSONEncoder # print(json.dumps(p.__dict__)) # 不认识对象 但认识字典 # 异常处理 # 在程序运行过程中 由于编码不规范等造成程序无法正常执行 此时程序会报错 # 为了保证程序的健壮性 # 很多编程语言都有异常处理机制 # 避免一错在错 def div(a,b): return a / b # 这样程序不会直接崩掉 try: x = div(5,0) except Exception as e: # 程序如果出错 会立即跳转到except语句 print('程序出错了') print(e) else: # 程序运行没出错 会执行else里的语句代码 print('计算结果是',x) print(22) # try...except 语句用来处理程序运行过程中的异常 # # 还可以处理指定错误 # 异常的使用场景 # age = input('请输入你的年龄:') # if int(age) > 18: # print('欢迎来到我的网站') # 如果用户输入的不是年龄 程序就会报错 age = input('请输入你的年龄:') try: age = float(age) # 转换成数字 except Exception as e: # 转换出错的话 # except ValueError as e: # 指定类型的错误 print('输入的不是数字') else: if int(age) > 18: print('欢迎来到我的网站') else: print('未满18')
    Processed: 0.022, SQL: 8