Python之路 day2 按行读文件

时间:2021-10-31 13:15:16
 1 #1. 最基本的读文件方法:
 2 
 3 # File: readline-example-1.py
 4  
 5 file = open("sample.txt")
 6  
 7 while 1:
 8     line = file.readline()
 9     if not line:
10         break
11     pass # do something
12   #一行一行得从文件读数据,显然比较慢;不过很省内存。
13 
14   #在我的机器上读10M的sample.txt文件,每秒大约读32000行
15 
16 #2. 用fileinput模块
17 
18 # File: readline-example-2.py
19  
20 import fileinput
21  
22 for line in fileinput.input("sample.txt"):
23     pass
24   #写法简单一些,不过测试以后发现每秒只能读13000行数据,效率比上一种方法慢了两倍多……
25 
26 #3. 带缓存的文件读取
27 
28 # File: readline-example-3.py
29  
30 file = open("sample.txt")
31  
32 while 1:
33     lines = file.readlines(100000)
34     if not lines:
35         break
36     for line in lines:
37         pass # do something
38   #这个方法真的更好吗?事实证明,用同样的数据测试,它每秒可以读96900行数据!效率是第一种方法的3倍,第二种方法的7倍!
39 
40 ————————————————————————————————————————————————————————————
41 
42 #在Python 2.2以后,我们可以直接对一个file对象使用for循环读每行数据:
43 
44 # File: readline-example-5.py
45  
46 file = open("sample.txt")
47  
48 for line in file:
49     pass # do something
50   #而在Python 2.1里,你只能用xreadlines迭代器来实现:
51 
52 # File: readline-example-4.py
53  
54 file = open("sample.txt")
55  
56 for line in file.xreadlines():
57     pass # do something