day02 文件处理和函数基础
以下是今日学习主要内容
# # 1.数据类型剩余内置方法
# # 2.字符编码
# 说明:本程序在注册基础上写出,目的是提升文件操作和字符处理能力,锻炼逻辑能力 def login(): with open('吴迎峰.txt','r',encoding = 'utf-8') as f: res = f.read() print(res.split(';')) username, password = res.split(';') while True: i = 1 j = 4 while i: user = input('请输入用户名:').strip() if user == username: while j: pwd = input('请输入密码:').strip() if pwd == password: print('登陆成功') break else: j -= 1 if j > 1: print('请重新输入密码') else: print('请重新输入用户名') break i = 0 else: print('用户名不存在') login()
# # 3.文件处理
# # 4.函数基础
#
# # 需要掌握的方法
# list1 = ["lee",21,"boss","zym",[1,2,3]]
#
# # insert()
# # 第一个参数:索引,第二个参数,插入内容
# # list1.insert(3,"zym")
# print(list1)
#
# # pop()
#
# # remove()
#
# # count()
# print(list1.count("zym"))
#
# # index()
# print(list1.index(21))
#
# # copy() 浅拷贝(只拷贝浅层内容,深层仍是地址转移)
# # copy为完全拷贝当前内容,内存地址和之前不一致,
# # 赋值为内存地址的指向,之前一样所以可以动态更新
# from copy import deepcopy
# list2 = list1.copy()
# list3 = list1
# list4 = deepcopy(list1)
# print(list1,"原列表")
# print(list3,"赋值")
# print(list2,"浅拷贝")
# print(list4,"深拷贝")
#
# # 对外层列表和内层列表进行拓展
# list1.append("lala")
# list1[5].append("didi")
#
# # 可以看出,深拷贝多层都为拷贝而不是地址空间的复制
# print(list1,"原列表")
# print(list3,"赋值")
# print(list2,"浅拷贝")
# print(list4,"深拷贝")
#
# # clear()
# print(list1.clear())
#
# # extend()
# list1_1 = [1,2,3]
# list1_2 = [4,5,6]
# list1_1.extend(list1_2)
# print(list1_1)
#
# # reverse()
# list1_1.reverse()
# print(list1_1)
#
# # sort()
# list1_3 = [8,6,3,5,9,1,]
# print(list1_3)
# list1_3.sort()
# print(list1_3)
# list1_3.sort(reverse=True)
# print(list1_3)
#
# # ===============================================================
# # ===============================================================
# # 字典
# dict1 = {'name':'lee','age':21,'sex':'male','school':'ahpu'}
#
# # 按照key存取值
# print(dict1["school"])
# # get
# print(dict1.get("name"))
# print(dict1.get("nam"))
# print(dict1.get("name","国立武汉大学"))
# print(dict1.get("nam","111"))
#
# # len
# print(len(dict1))
#
# # in or not
# print("lee" in dict1)
# print("name" in dict1)
#
# # delete
# del dict1["school"]
# print(dict1)
#
# # pop
# name1 = dict1.pop("name")
# print(dict1)
# print(name1)
# # popitem随即取出字典中的某个元素
# # (字典是无序类型)
# dict1.popitem()
# print(dict1)
#
# # key value items
# print(dict1.keys())
# print(dict1.values())
# print(dict1.items())
#
# # 循环
# for key in dict1:
# print(key)
#
# # update
# dict2 = {"n":"n","B":"b"}
# dict1.update(dict2)
# print(dict1)
#
# # 元组(小括号内,逗号隔开,存放多个值)
# # 注意元组与列表的区别
# # 元组是不可变类型,列表是可变类型
# t1 = (1, 2, 3, 4, 5, 6, 7, 8, 9)
# # 索引取值,
#
#
# # 切片
# print(t1[0:10:2])
# print(t1[0:10])
#
# # 长度
# print(len(t1))
#
# # 成员运算
# print(2 in t1)
# print(22 in t1)
#
# # 循环
# for i in t1:
# print(i)
#
# # 集合(在大括号内,以逗号隔开,
# # 可存放多个值,自带默认去重复功能)
# set1_1 = {1,3,4,5,6,4,4,3}
# print(set)
#
# set1 = set()
# set2 = {}
# print(set1)
# print(set2)
# print(type(set1))
# print(type(set2))
#
# # 文件处理
# # 读写基本使用
# # 对文本进行操作
# # f = open('name.txt',mode='wt',encoding="utf-8")
# # 绝对路径 模式 字符编码
# # f为句柄
# # write
# a = open(
# r'C:\Users\71936\Desktop\name.txt',
# mode='wt',
# encoding="utf-8")
# a.write('Hello World')
# a.close()
# # read
# b = open(
# r'C:\Users\71936\Desktop\name.txt',
# mode='rt',
# encoding="utf-8")
# res = b.read()
# print(res)
# b.close()
# # 文件追加
# c = open(
# r'C:\Users\71936\Desktop\name.txt',
# mode='a',#默认at模式
# encoding="utf-8")
# c.write('\nHello Lee')
# c.close()
# b = open(
# r'C:\Users\71936\Desktop\name.txt',
# mode='rt',
# encoding="utf-8")
# res = b.read()
# print(res)
# b.close()
# # 文件处理之上下文管理
# # 自动调用close方法
# # 写
# with open(
# r'C:\Users\71936\Desktop\name1.txt',
# 'w',
# encoding = "utf-8")as e:
# e.write("slkdjf")
# # 读
# with open(
# r'C:\Users\71936\Desktop\name1.txt',
# 'r',
# encoding = "utf-8")as f:
# demo = f.read()
# print(demo)
# # 追加
# with open(
# r'C:\Users\71936\Desktop\name1.txt',
# 'a',
# encoding = "utf-8")as g:
# g.write("\n11111")
# with open(
# r'C:\Users\71936\Desktop\name1.txt',
# 'r',
# encoding = "utf-8")as f:
# demo = f.read()
# print(demo)
# 图片与视频读写操作
#
# import requests
# res = requests.get('http://pic37.nipic.com/20140113/8800276_184927469000_2.png')
# print(res.content)
# # 写
# with open(r'C:\Users\71936\PycharmProjects\untitled\1.png','wb')as h:
# h.write(res.content)
# # 读
# with open(r'C:\Users\71936\PycharmProjects\untitled\1.png','rb')as i:
# u = i.read()
# print(u)
# # 拷贝
# with open(r'C:\Users\71936\PycharmProjects\untitled\2.png','wb')as j:
# j.write(u)
# # 读写视频
# with open(r'C:\Users\71936\PycharmProjects\untitled\1.mp4','rb') as a, open(r'C:\Users\71936\PycharmProjects\untitled\2.mp4','wb') as b:
# res = a.read()
# print(res)
# b.write(res)
# # 一行一行读文件
# with open(r'1.mp4','rb') as a, open(r'2.mp4','wb') as b:
# a.read()
# for line in a:
# a.write(line)
# '''
# =======================================================
# 函数
# =======================================================
# def 用来生命定义函数的关键字
# 函数名:看其名知其意
# ():括号,存放的是接受外界数据的参数
# 注释:用来说明函数的作用
# 函数体代码:逻辑代码
# return 返回值
# '''
# # 注册功能
# def register():
# while True:
# user = input('请输入用户名:').strip()
# pwd = input('请输入密码:').strip()
# re_pwd = input('请再次输入密码:').strip()
#
# # 判断密码是否一致
# if pwd == re_pwd:
# # user_info = '用户名:%s,密码:%s' %(user,pwd)
# # user_info = '用户名:{},密码:{}'.format(user,pwd)
# user_info = f'用户名:{user},密码:{pwd}'
# # 把用户信息写入文件中
# with open(f'{user}.txt', 'w', encoding = 'utf-8') as f:
# f.write(user_info)
# break
# else:
# print('两次输入不一致')
# # 调用
# register()
# 函数在定义阶段发生的事情
# 1.先打开python解释器
# 2.加载.py文件
# 3.会检测语法,不会执行函数体代码
def fool():
print("you are a fool")
bar()
#
fool()