Python 如何将字符串替换为空字符串(在文件中)
我有一个包含以下文本的文件:Python 如何将字符串替换为空字符串(在文件中),python,Python,我有一个包含以下文本的文件: Oh, speak again, bright angel, for thou art As glorious to this night, being o'er my head As is a winged messenger of heaven Unto the white-upturned wondering eyes Of mortals that fall back to gaze on him When he bestrides
Oh, speak again, bright angel, for thou art
As glorious to this night, being o'er my head
As is a winged messenger of heaven
Unto the white-upturned wondering eyes
Of mortals that fall back to gaze on him
When he bestrides the lazy-puffing clouds
And sales upon the bosom in the air
任务是:我必须将以“As”开头的行替换为空行,然后将输出保存到新文件中。到目前为止,我已经想出了如何替换这些词。
这是我最后的代码:
def change_strings():
with open ("file1.txt") as file1:
strings = file1.readlines()
for string in strings:
if not string.startswith("As"):
with open("file2.txt", "w") as file2:
file2.write(string)
但是,我只将最后一行保存到新文件中。我做错了什么?对于循环中的每个步骤,您都在重新打开同一个文件,截断它(删除所有内容),并在现在已空的文件中写入一行 您需要以附加模式(“a”而不是“w”)打开文件,以便每次将该行附加到当前现有的内容 或者,更好的办法是,在循环之外只打开一次,然后写下所有需要的行:
def change_strings():
with open ("file1.txt") as file1:
with open("file2.txt", "w") as file2:
strings = file1.readlines()
for string in strings:
if not string.startswith("As"):
file2.write(string)
对于循环中的每个步骤,您将重新打开同一个文件,截断它(删除所有内容),并在现在为空的文件中写入一行 您需要以附加模式(“a”而不是“w”)打开文件,以便每次将该行附加到当前现有的内容 或者,更好的办法是,在循环之外只打开一次,然后写下所有需要的行:
def change_strings():
with open ("file1.txt") as file1:
with open("file2.txt", "w") as file2:
strings = file1.readlines()
for string in strings:
if not string.startswith("As"):
file2.write(string)
这就是我在评论中的意思——不要重新打开并覆盖输出文件,只打开一次 还请注意,不需要
.readlines()
;这将在内存中读取您的整个文件,如果您的文件非常大,那么这可能不是您想要的
from io import StringIO
text = '''Oh, speak again, bright angel, for thou art
As glorious to this night, being o'er my head
As is a winged messenger of heaven
Unto the white-upturned wondering eyes
Of mortals that fall back to gaze on him
When he bestrides the lazy-puffing clouds
And sales upon the bosom in the air
'''
with StringIO(text) as infile, open('out.txt', 'w') as outfile:
for line in infile:
if not line.startswith("As"):
outfile.write(line)
…当然,您需要将
StringIO(text)
替换为open('file1.txt','r')
。这只是为了让我的示例自我包含。这就是我在评论中的意思-而不是重新打开并覆盖输出文件,只打开一次
还请注意,不需要.readlines()
;这将在内存中读取您的整个文件,如果您的文件非常大,那么这可能不是您想要的
from io import StringIO
text = '''Oh, speak again, bright angel, for thou art
As glorious to this night, being o'er my head
As is a winged messenger of heaven
Unto the white-upturned wondering eyes
Of mortals that fall back to gaze on him
When he bestrides the lazy-puffing clouds
And sales upon the bosom in the air
'''
with StringIO(text) as infile, open('out.txt', 'w') as outfile:
for line in infile:
if not line.startswith("As"):
outfile.write(line)
…当然,您需要将
StringIO(text)
替换为open('file1.txt','r')
。这只是为了让我的示例自我包含。您可以为每一行重新打开写入的文件。。。只打开一次(在原始的中使用
语句,应该没问题。在编写代码之前,请阅读Python文档。打开
函数的“w”
标志意味着“打开一个文件进行写入,破坏该文件的任何现有内容”。每行重新打开写入的文件…只打开一次(在原始的语句中,使用语句),应该可以。在编写代码之前,请阅读Python文档。打开函数的“w”
标志表示“打开文件进行写入,破坏文件的任何现有内容”。您可以使用将打开(填充)作为文件1,将打开(填充)作为文件2来保存缩进级别。您可以使用将打开(填充)作为文件1,将打开(填充)作为文件2来保存缩进级别作为文件2
。主题:是武士佩剑披萨送货员!@Thomas抓到我了!来自YT的问候。主题:是武士佩剑披萨送货员!@Thomas抓到我了!来自YT的问候。