如何将某些文本复制到Python的某个行中?

发布于 2025-02-05 04:44:50 字数 316 浏览 1 评论 0原文

好吧,关于将文本复制和粘贴到文件中的帖子很多,但我想将其添加到我将其粘贴到的代码中的特定行中。

例如,我将此文件命名为“ main.leap”,内容看起来完全像这样:

outln "Hello";

我还有另一个名为“ main.py”的文件,并且内容看起来完全像这样:

text_input = """

"""

print(text_input)

我如何使用intimpts suntleil之类的进口来粘贴Main.Main.Leap的内容特别是在第2行中,没有其他行?

Alright, there are so many posts about copying and pasting text into a file, but I want to add it to a SPECIFIC line inside of the code that I'm pasting it into.

For example, I have this file named 'main.leap' and the contents look exactly like this:

outln "Hello";

And I have another file named 'main.py' and the contents look exactly like this:

text_input = """

"""

print(text_input)

How can I use imports like Shutil to paste the contents of main.leap SPECIFICALLY inside line 2 and in no other line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2025-02-12 04:44:51

没有封闭式:

main = open('main.py')
lines = main.readlines()
main.close()

leap = open('main.leap')
newline = leap.read()
leap.close()

lines.insert(1, newline)

main = open('main.py', 'w')
main.writelines(lines)
main.close()

Without shutil:

main = open('main.py')
lines = main.readlines()
main.close()

leap = open('main.leap')
newline = leap.read()
leap.close()

lines.insert(1, newline)

main = open('main.py', 'w')
main.writelines(lines)
main.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文