• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python3读取UTF-8文件及统计文件行数的方法

Python3读取UTF-8文件及统计文件行数的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python3读取txt文件,python3读取文件,python3读取csv文件,python3 读取mat文件,java 读取文件行数等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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) 

</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • python3读取MySQL-Front的MYSQL密码
  • python3读取MySQL-Front的MYSQL密码
  • Python3处理文件中每个词的方法
  • Python3读取UTF-8文件及统计文件行数的方法
  • Python3读取zip文件信息的方法

相关文章

  • Python通过递归遍历出集合中所有元素的方法
  • python利用elaphe制作二维条形码实现代码
  • 利用Python中的pandas库对cdn日志进行分析详解
  • python中解析json格式文件的方法示例
  • Python HTTP客户端自定义Cookie实现实例
  • tensorflow查看ckpt各节点名称实例
  • 在Python中使用正则表达式的方法
  • Python遍历目录并批量更换文件名和目录名的方法
  • Python读取图片EXIF信息类库介绍和使用实例
  • python3中int(整型)的使用教程

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python encode和decode的妙用
    • 详解Python编程中对Monkey Patch猴子补丁开发方式的运用
    • python sys模块sys.path使用方法示例
    • Python制作简易注册登录系统
    • python getopt详解及简单实例
    • python启动办公软件进程(word、excel、ppt、以及wps的et、wps、wpp)
    • python 读写txt文件 json文件的实现方法
    • python删除列表内容
    • python爬取本站电子书信息并入库的实现代码
    • python中sys.argv参数用法实例分析

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有