小文件读取,计算行数
1.
count = len(open(filepath,'rU').readlines())
大文件读取,计算行数
2.
count = -1
for count, line in enumerate(open(thefilepath, 'rU')):
pass
count += 1
3.
count = 0
thefile = open(thefilepath, 'rb')
while True:
buffer = thefile.read(8192*1024)
if not buffer:
break
count += buffer.count('\n')
thefile.close( )
读取指定行
import linecache
count = linecache.getline(filename,linenum)
传统方式(遍历):
1:readline()
1
2
3
4
5
6
7
|
file = open ( "sample.txt" ) while 1 : line = file .readline() if not line: break pass # do something file .close() |
一行一行得从文件读数据,显然比较慢;
不过很省内存;
测试读10M的sample.txt文件,每秒大约读32000行;
2:fileinput
1
2
3
|
import fileinput for line in fileinput. input ( "sample.txt" ): pass |
写法简单一些,不过测试以后发现每秒只能读13000行数据,效率比上一种方法慢了两倍多;
3:readlines()
1
2
3
4
5
6
7
8
|
file = open ( "sample.txt" ) while 1 : lines = file .readlines( 100000 ) if not lines: break for line in lines: pass # do something file .close() |
用同样的数据测试,它每秒可以读96900行数据!效率是第一种方法的3倍,第二种方法的7倍!
4:文件迭代器
每次只读取和显示一行,读取大文件时应该这样:
1
2
3
4
|
file = open ( "sample.txt" ) for line in file : pass # do something file .close() |
附加代码:
#coding=utf-8
#!文件类型: python
#!创建时间: 2018/11/12 14:03
#!作者: SongBin
#!文件名称: test.py
import random
import linecache
filepath = "myblog.txt"
#总行数
count = len(open(filepath,'rU').readlines())
print("总行数:"+str(count))
#在1到总行数间取随机数
rnum = random.randrange(1, count, 1)
print("随机选定行:"+str(rnum))
#取随机选定行内容
str = linecache.getline(filepath,rnum)
print("选定行内容:"+str)
更多内容请访问:IT源点
注意:本文归作者所有,未经作者允许,不得转载