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

 

1、

root@PC1:/home/test2# ls
a.txt  test.py
root@PC1:/home/test2# cat a.txt            ## 测试数据
z u
x e j
z
e       f       a
z x e
w f e
root@PC1:/home/test2# cat test.py          ## python脚本
#!/usr/bin/python

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

lines = in_file.readlines()

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

for i in lines:

    print("columns: ",  i.count(" ") + i.count("\t") + 1)

in_file.close()
root@PC1:/home/test2# python test.py   
rows:  6          ## 行数
columns:  2       ## 每行的列数
columns:  3
columns:  1
columns:  3
columns:  3
columns:  3

 

posted @ 2022-05-23 10:02  小鲨鱼2018  阅读(373)  评论(0编辑  收藏  举报