开发者社区> 问答> 正文

python怎么计算文档的行数据库

python怎么计算文档的行数据库

展开
收起
云计算小粉 2018-05-10 20:10:59 1633 0
1 条回答
写回答
取消 提交回答
  • 最方便的语言,最简洁的简介

    小文件读取,计算行数

    1. count = len(open(filepath,'rU').readlines())

    大文件读取,计算行数

    1. count = -1
         for count, line in enumerate(open(thefilepath, 'rU')):

           pass
       count += 1

    1. 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)

    2019-07-17 22:24:16
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载
云时代的数据库技术趋势 立即下载
超大型金融机构国产数据库全面迁移成功实践 立即下载