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 @ 2022-05-31 23:02  小鲨鱼2018  阅读(1074)  评论(0编辑  收藏  举报