open()函数
'''文件处理open函数的用法:读、写、追加模式
文件处理写
f = open("1.py","w",encoding="utf-8")
data = f.write("111
")
f.close()
文件处理读
r = open("1.py","r",encoding="utf-8")
data = r.read()
print(data)
r.close()
文件处理追加
a = open("1.py","a",encoding="utf-8")
data = a.write("2222
")
a.close()
with的用法:可以省略掉python不释放内存的问题
文件处理读
with open("1.py","r",encoding="utf-8") as r:
print(r.read())
文件处理写
with open("1.py","w",encoding="utf-8") as w:
w.write("3333
")
文件处理追加
with open("1.py","a",encoding="utf-8") as a:
a.write("4444
")
将源文件写入新的文件中
with open("1.py","r",encoding="utf-8") as r ,open("2.py","w",encoding="utf-8") as w:
w.write(r.read())
追加文件到源文件中
with open("1.py","r",encoding="utf-8") as r ,open("1.py","a",encoding="utf-8") as a:
a.write("11111
")
data = r.read()
print(data)
b模式
b模式的读、写、追加
文件读模式
f = open("1.py","rb")#b模式不需要指定编码格式
data = f.read()
print(data) #查看到是bytes格式
print(data.decode("utf-8")) #解码
转换模式:
字符串----------encode-----------bytes
bytes-----------decode-----------字符串
文件写模式
f = open("1.py","wb")
f.write(bytes("22222
",encoding="utf-8"))
f.write(bytes("张三
",encoding="utf-8")) #编码方法1
f.write("李四".encode("utf-8")) #编码方法2
文件追加模式
a = open("1.py","ab")
a.write(bytes("张三
",encoding="utf-8"))
a.write("李四
".encode("utf-8"))
'''
# 其他方法:
# f = open("1.py","r",encoding="utf-8",newline="") #读取文件中真正的换行符合
# print(f.readlines())
# print(f.tell()) #光标位置
# f.seek(3) #指定光标所在位置(以字节为单位)
# f.read(3) #代表读取3个字符
# f.truncate() #文件截断,以r+、a+模式打开,w+模式不可以,会清空文件
# 文件操作倒着查找:
# f = open ("日志文件","rb")
# 方式一:
# data = f.readlines()
# print(data[-1].decode("utf-8")) #切片方式,截取最后一段
# 方式二:
# for i in f.readlines(): #for循环以列表形式一行一行读取打开的文件
# print(i)
# for i in f:
# offs = -10 #定义一个倒序变量
# while True:
# f.seek(offs,2) #以2倒序模式读取10个字节
# data = f.readlines() #一行一行读取打开的文件,组成列表
# if len(data) > 1: #判断读取的行数是否大于1,如果小于1,则offs*=2增加一倍读取
# print("文件的最后一行是%s" %(data[-1].decode("utf-8")))
# break
# offs *=2 #读取行数小于一行,说明字节数不够,增加一倍,倒数20个字节
#三元表达式
# name = "alex"
# name = "tom"
# res="low货" if name == "alex" else "帅锅"
# print(res)
#列表解析 还可以结合三元表达式,结合判断;
# 常规方法一
# egg_list = []
# for i in range(10):
# egg_list.append("蛋%s" %i)
# print(egg_list)
#列表解析方法二
# l = ["蛋%s" %i for i in range(10)]
# print(l)
# 生成器表达式 省内存
# l_m = ("鸡蛋%s" %i for i in range(10))
# # 每次生成一个,next方法
# # 方法一
# print(l_m.__next__())
# # 方法二
# print(next(l_m)) #next本质就是调用__next__
生成器函数:yield
1 import time 2 def test(): 3 print('开始打印') 4 yield 1 5 time.sleep(3) 6 yield 2 7 time.sleep(3) 8 yield 3 9 res = test() 10 print(res) 11 print(res.__next__()) #nest方式 12 print(next(res)) 13 print(next(res))
人口普查py
1 #人口表 2 {'name':'北京','population':10} 3 {'name':'山东','population':20} 4 {'name':'山西','population':30} 5 {'name':'西安','population':40} 6 {'name':'河北','population':50}
1 #人口统计和百分比 2 #函数 3 def get_population(): 4 with open("人口","r",encoding="utf-8") as f: #打开一张表,相对路径 5 for i in f : 6 yield i 7 g = get_population() #函数执行第一次 8 g1 = get_population() #函数执行第二系 9 #调用第一次执行,求出总人数 10 all_pop = sum(eval(i)["population"] for i in g) 11 print("总人口%s" % all_pop) 12 #调用第二次函数执行,求出每个省对应人数 13 s1 = g1.__next__() 14 print("%s"'%.2f %%' % (eval(s1)["name"] ,eval(s1)["population"] / all_pop)) 15 s2 = g1.__next__() 16 print('%s''%.2f %%' % (eval(s2)["name"] ,eval(s2)["population"] / all_pop)) 17 s3 = g1.__next__() 18 print('%s''%.2f %%' % (eval(s3)["name"] ,eval(s3)["population"] / all_pop)) 19 s4 = g1.__next__() 20 print('%s''%.2f %%' % (eval(s4)["name"] ,eval(s4)["population"] / all_pop)) 21 s5 = g1.__next__() 22 print('%s''%.2f %%' % (eval(s5)["name"] ,eval(s5)["population"] / all_pop))