python中统计文本的行数及列数

 

1、

复制代码
[root@PC1 test]# ls
a.txt  test.py
[root@PC1 test]# cat a.txt
U R D
S F E
A D E j k
x v m   j
e f x
e       r d k j k u
z       d       v
[root@PC1 test]# cat test.py        ## 统计行数和列数
#!/usr/bin/python

in_file = open("a.txt", "r")

lines = in_file.readlines()

print("rows:", len(lines))

j = 0
for i in lines:
    j = j + 1
    print("row%s_cols%s" % (j, ":"), i.count(" ") + i.count("\t") + 1)

in_file.close()
[root@PC1 test]# python test.py
rows: 7
row1_cols: 3
row2_cols: 3
row3_cols: 5
row4_cols: 4
row5_cols: 3
row6_cols: 7
row7_cols: 3
复制代码

 

posted @   小鲨鱼2018  阅读(1100)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-05-31 c语言中strncat函数
2021-05-31 c语言中strcat函数
2021-05-31 c语言中strncpy函数
2021-05-31 c语言中字符串的复制
2021-05-31 c语言 11-6
2021-05-31 c语言 11-5
2021-05-31 c语言 11-4
点击右上角即可分享
微信分享提示