繁体   English   中英

在Python中:我需要用一首诗作为文本文件,并创建一个副本,但编号为1、2,等

[英]In Python: I need to take a text file with a poem and create a copy but with numbered lines 1., 2., ect

我只是刚刚开始,我不确定如何将数字放在txt文件中的每一行的前面。 到目前为止,这就是我所拥有的。

def编号(infile,outfile):infile中的行:line =#我不确定从这里到哪里

def main():

try:
    infileName = input("Enter the file holding the poem:")
    infile = open(infileName,"r")

    outfileName = input("Enter the file name to hold the numbered poem:")
    outfile = open(outfileName,"w")
    print("Numbered version of" + infileName + "is here in" + outfileName +)
except IOError:
    print ("Error, could not find files")

主要()

最终结果应该是诗歌的第二行第一行,诗歌的第三行

分为:1.诗歌第一行2.诗歌第二行3.诗歌第三行

我相信这会有所帮助:

poem = open('first.txt', 'r')
output = open('second.txt', 'w')
count = 1
for line in poem.readlines():
    output.write(str(count) + " " + line + "\n")
    count += 1
poem.close()
output.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM