Python3读取UTF-8文件及统计文件行数的方法,
Python3读取UTF-8文件及统计文件行数的方法,
本文实例讲述了Python3读取UTF-8文件及统计文件行数的方法。分享给大家供大家参考。具体实现方法如下:
''''' Created on Dec 21, 2012 Python 读取UTF-8文件 统计文件的行数目 @author: liury_lab ''' # -*- coding: utf-8 -*- import codecs # 对较小的文件,最简单的方法是将文件读入一个行列表中, # 然后计算列表的长度即可 count = len(codecs.open('d:/FreakOut.cpp', 'rU', 'utf-8').readlines()) print(count) # 对较大的文件,可循环计数 count = -1 for count, line in enumerate(codecs.open('d:/FreakOut.cpp', 'rU', 'utf-8')): pass count += 1 print(count) # 对于像windows结束标记有'\n'的,还可以有如下办法: count = 0 the_file = codecs.open('d:/FreakOut.cpp', 'rb', 'utf-8') while (True): buffer = the_file.read(8192*1024) if not buffer: break count += buffer.count('\n') count += 1 the_file.close() print(count)
希望本文所述对大家的Python程序设计有所帮助。
相关内容
- Python3处理文件中每个词的方法,
- python实现数组插入新元素的方法,python新元素
- python实现的简单抽奖系统实例,python抽奖实例
- linux下python抓屏实现方法,python抓屏
- python使用reportlab实现图片转换成pdf的方法,
- Python中用sleep()方法操作时间的教程,pythonsleep
- 详解Python中的strftime()方法的使用,pythonstrftime
- 在Python中操作时间之strptime()方法的使用,pythonstrptime
- 详解Python中time()方法的使用的教程,pythontime
- 在Python中操作时间之tzset()方法的使用教程,pythontzset
评论关闭