源码

PYTHON-在输出中添加行号


例如,如果输入文件是:

def main():
    for i in range(10):
        print("I love python")
    print("Good bye!")

然后输出将是:

1   def main():
2       for i in range(10):
3           print("I love python")
4       print("Good bye!")

我很难在每行添加行.请帮忙!我的计划是:

UPDATED***

filename = input("Please enter a file name: ")
count = 0

openfile = open(filename, "r")

for lines in openfile:
    linenumbers = openfile.write(str(count)+'/t'+lines)
    count += 1


print(count)
(1)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/python-zaishuchuzhongtianjiaxinghao.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 26, 2019 at 08:31 下午

热评文章