python读取txt文件,去掉空格计算每行长度的方法

yipeiwu_com6年前Python基础

如下所示:

# -*- coding: utf-8 -*-

file2 = open("source.txt", 'r')

file1 = open("target.txt", "r")

for value1 in file1.readlines():
 word1 = str(value1).split()
 l1 = len(word1)
 print l1
for value2 in file2.readlines():
 word2 = str(value2).split()
 l2 = len(word2)
 print l2
file1.close()
file2.close()

以上这篇python读取txt文件,去掉空格计算每行长度的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Pycharm导入Python包,模块的图文教程

Pycharm导入Python包,模块的图文教程

1、点击File->settings 2、选择Project Interpreter,点击右边绿色的加号添加包 3、输入你想添加的包名,点击Install Package 4...

python库matplotlib绘制坐标图

很多时候我们数据处理的时候要画坐标图,下面我用第三方库matplotlib以及scipy绘制光滑的曲线图 需要安装的库有 matplotlib,scipy, numpy impor...

Python入门之三角函数atan2()函数详解

描述 atan2() 返回给定的 X 及 Y 坐标值的反正切值。 语法 以下是 atan2() 方法的语法: import math math.atan2(y, x) 注意:...

python中enumerate的用法实例解析

在python中enumerate的用法多用于在for循环中得到计数,本文即以实例形式向大家展现python中enumerate的用法。具体如下: enumerate参数为可遍历的变量,...

python实现基本进制转换的方法

本文实例讲述了python基本进制转换的方法。分享给大家供大家参考。具体如下: # Parsing string with base into a number is easy nu...