python计算文件的行数和读取指定行的内容
发布日期:2021-05-07 19:07:35 浏览次数:13 分类:技术文章

本文共 379 字,大约阅读时间需要 1 分钟。

小文件读取,计算行数

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)

上一篇:爬虫数据老不好,需要缓存来断点续爬实现
下一篇:Python3网络爬虫(六):Python3使用Cookie-模拟登陆获取妹子联系方式

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年03月30日 03时32分38秒