VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • python基础教程之day18-python之迭代器和生成器

1.文件处理模式b模式

复制代码
 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 # f=open('test.py','rb',encoding='utf-8') #b的方式不能指定编码
 4 # f=open('test.py','rb') #b的方式不能指定编码
 5 # data=f.read()
 6 # #'字符串'---------encode---------》bytes
 7 # #bytes---------decode---------》'字符串'
 8 # print(data)
 9 # print(data.decode('utf-8'))
10 # f.close()
11 
12 
13 # f=open('test.py','wb') #b的方式不能指定编码
14 # f.write(bytes('1111\n',encoding='utf-8'))
15 # f.write('杨件'.encode('utf-8'))
16 
17 # f=open('test.py','ab') #b的方式不能指定编码
18 # f.write('杨件'.encode('utf-8'))
19 
20 # open('a;ltxt','wt')
复制代码

2.读取大文件最后一行

复制代码
 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 f=open('test.txt','rb')
 4 
 5 for i in f:
 6     offs=-3
 7     n=0
 8     while True:
 9         f.seek(offs,2)
10         data=f.readlines()
11         if len(data) > 1:
12             print('最后一行',data[-1].decode("utf-8"))
13             break
14         offs*=2
复制代码

3.文件操作的其他方法

复制代码
  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 # f=open('a.txt','r+',encoding='utf-8')
  4 # data=f.read()
  5 # print(data)
  6 # f.write('你好')
  7 
  8 # f=open('a.txt','r+',encoding='latin-1')
  9 # data=f.read()
 10 # print(data)
 11 # f.write('aaaaaaaaaaa')
 12 
 13 # f=open('a.txt','r',encoding='utf-8',newline='') #读取文件中真正的换行符号
 14 f=open('a.txt','r+',encoding='utf-8',newline='') #读取文件中真正的换行符号
 15 #
 16 # print(f.closed)
 17 # print(f.encoding)
 18 # f.flush()
 19 # print(f.readlines())
 20 
 21 # print(f.tell())
 22 # f.readline()
 23 # print(f.tell())
 24 #
 25 # f.seek(1)
 26 # print(f.tell())
 27 # print(f.readlines())
 28 # f.seek(3)
 29 # print(f.tell())
 30 # print(f.read())
 31 
 32 # data=f.read(1)
 33 # print(data)
 34 
 35 f.truncate(10)
 36 
 37 
 38 # f.flush() #讲文件内容从内存刷到硬盘
 39 #
 40 # f.closed #文件如果关闭则返回True
 41 #
 42 # f.encoding #查看使用open打开文件的编码
 43 # f.tell() #查看文件处理当前的光标位置
 44 #
 45 # f.seek(3) #从开头开始算,将光标移动到第三个字节
 46 # f.truncate(10) #从开头开始算,将文件只保留从0-10个字节的内容,文件必须以写方式打开,但是w和w+除外
 47 #
 48 # f=open('a.txt','r',newline='')
 49 #
 50 # data=f.readline().encode('utf-8')
 51 # print(data)
 52 # print(f.tell())
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 # f=open('seek.txt','r',encoding='utf-8')
 61 # print(f.tell())
 62 # f.seek(10)
 63 # print(f.tell())
 64 # f.seek(3)
 65 # print(f.tell())
 66 
 67 # f=open('seek.txt','rb')
 68 # print(f.tell())
 69 # f.seek(10,1)
 70 # print(f.tell())
 71 # f.seek(3,1)
 72 # print(f.tell())
 73 
 74 
 75 # f=open('seek.txt','rb')
 76 # print(f.tell())
 77 # f.seek(-5,2)
 78 # print(f.read())
 79 # print(f.tell())
 80 # f.seek(3,1)
 81 # print(f.tell())
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 #
 91 # f=open('日志文件.txt','rb')
 92 # data=f.readlines()
 93 # print(data[-1].decode('utf-8'))
 94 
 95 f=open('日志文件.txt','rb')
 96 #
 97 # for i in f.readlines():
 98 #     print(i.decode('utf-8'))
 99 
100 #循环文件的推荐方式
101 # for i in f:
102 #     print(i)
103 
104 for i in f:
105     offs=-10
106     while True:
107         f.seek(offs,2)
108         data=f.readlines()
109         if len(data) > 1:
110             print('文件的最后一行是%s' %(data[-1].decode('utf-8')))
111             break
112         offs*=2
复制代码

4.迭代器和生成器

复制代码
  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 
  4 
  5 
  6 # x='hello'
  7 # # print(dir(x))
  8 # iter_test=x.__iter__()
  9 # #
 10 # print(iter_test)
 11 # print(iter_test.__next__())
 12 # print(iter_test.__next__())
 13 # print(iter_test.__next__())
 14 # print(iter_test.__next__())
 15 # print(iter_test.__next__())
 16 # print(iter_test.__next__())
 17 
 18 # l=[1,2,3]
 19 # for i in l:  #i_l=l.__iter_()  i_l.__next__()
 20 #     print(i)
 21 
 22 # index=0
 23 # while index < len(l):
 24 #     print(l[index])
 25 #     index+=1
 26 
 27 
 28 # iter_l=l.__iter__() #遵循迭代器协议,生成可迭代对象
 29 # print(iter_l.__next__())
 30 # print(iter_l.__next__())
 31 #
 32 # for i in l:
 33 #     print(i)
 34 
 35 # s={1,2,3}
 36 #
 37 # # for i in s:
 38 # #     print(i)
 39 # iter_s=s.__iter__()
 40 # print(iter_s)
 41 # print(iter_s.__next__())
 42 # print(iter_s.__next__())
 43 # print(iter_s.__next__())
 44 # print(iter_s.__next__())
 45 
 46 # dic={'a':1,'b':2}
 47 # iter_d=dic.__iter__()
 48 # print(iter_d.__next__())
 49 
 50 # f=open('test.txt','r+')
 51 # # for i in f:
 52 # iter_f=f.__iter__()
 53 # print(iter_f)
 54 # print(iter_f.__next__(),end='')
 55 # print(iter_f.__next__(),end='')
 56 # l=[1,2,3,4,5]
 57 # diedai_l=l.__iter__()
 58 # while True:
 59 #     try:
 60 #         print(diedai_l.__next__())
 61 #     except StopIteration:
 62 #         # print('迭代完毕了,循环终止了')
 63 #         break
 64 
 65 # l=['die','erzi','sunzi','chongsunzi']
 66 # #
 67 # iter_l=l.__iter__()
 68 # print(iter_l)
 69 # print(iter_l.__next__())
 70 # print(iter_l.__next__())
 71 # print(iter_l.__next__())
 72 # print(iter_l.__next__())
 73 # print(iter_l.__next__())
 74 # print(next(iter_l)) #next()---->iter_l.__next__()
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 # def test():
 86 #     yield 1
 87 #     yield 2
 88 #     yield 3
 89 # g=test()
 90 # print('来自函数',g)
 91 # print(g.__next__())
 92 # print(g.__next__())
 93 # print(g.__next__())
 94 
 95 #三元表达式
 96 # name='alex'
 97 # # name='linhaifeng'
 98 # res='SB' if name == 'alex' else '帅哥'
 99 # print(res)
100 
101 
102 #列表解析
103 # egg_list=[]
104 # for i in range(10):
105 #     egg_list.append('鸡蛋%s' %i)
106 # print(egg_list)
107 
108 # l=['鸡蛋%s' %i for i in range(10)]
109 # # l1=['鸡蛋%s' %i for i in range(10) if i > 5 ]
110 # # l1=['鸡蛋%s' %i for i in range(10) if i > 5 else i] #没有四元表达式
111 # l2=['鸡蛋%s' %i for i in range(10) if i < 5] #没有四元表达式
112 #
113 # # print(l)
114 # # print(l1)
115 # print(l2)
116 
117 # laomuji=('鸡蛋%s' %i for i in range(10)) #生成器表达式
118 # print(laomuji)
119 # print(laomuji.__next__())
120 # print(laomuji.__next__())
121 # print(next(laomuji))
122 # print(next(laomuji))
123 # print(next(laomuji))
124 # print(next(laomuji))
125 # print(next(laomuji))
126 # print(next(laomuji))
127 # print(next(laomuji))
128 # print(next(laomuji))
129 # print(next(laomuji))
130 #
131 l=[1,2,3,34]
132 # map(func,l)
133 #
134 # print(sum(l))
135 # print(sum())
136 print(sum(i for i in range(10000000)))
复制代码


相关教程