0

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

def numbered(infile, outfile): for line in infile: line = #我不知道从这里去哪里

定义主():

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. 诗的第三行 等

4

1 回答 1

0

我相信这会有所帮助:

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()
于 2017-08-04T23:07:08.237 回答