首页 > 编程语言 > 详细

Python_添加行号

时间:2017-06-10 17:39:55      阅读:494      评论:0      收藏:0      [点我收藏+]
 1 filename=demo.py
 2 with open(filename,r)as fp:
 3     lines=fp.readlines() #读取所有行
 4 maxLength=max(map(len,lines))   #最长行的长度
 5 for index,line in enumerate(lines): #遍历所有行
 6     newLine=line.rstrip()   #删除每行右侧的空白字符
 7     newLine=newLine+ *(maxLength+5-len(newLine))  #在每行固定位置添加行号
 8     newLine=newLine+#+str(index+1)+\n   #添加行号
 9     lines[index]=newLine
10 with open(filename[:-3]+_new.py,w)as fp:    #将结果写入文件
11     fp.writelines(lines)

 

Python_添加行号

原文:http://www.cnblogs.com/cmnz/p/6979307.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!