繁体   English   中英

将文本附加到文本文件中的某个位置

[英]Append text into a text file into a certain position

def append_csv_file(filename):
    with open (filename, "a") as csv_file:
        file_name = csv_file
        appending = input("please enter the number of times you would like to enter "
                          "some text into the file on a different line: ")
        appending = int(appending)
        for appending in range(appending):
            x = input("please enter what you would like to append: ")
            file_name.write("\n" + x)

        exit()


xxx = input("please enter the file you would like to append to the file: ")
print(append_csv_file(xxx))

我如何使此代码附加到某一行

如果您想在现有文件中插入新行,您实际上必须重建文件(至少从那时起)。 这是因为从程序上讲,它下面的所有行都是不同的。

我要做的是将整个文件读入一个列表,然后将要创建的行合并到所述列表中,然后将输出写入一个新文件。

暂无
暂无

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

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