Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么';这不是简单的搜索工作吗?_Python - Fatal编程技术网

Python 为什么';这不是简单的搜索工作吗?

Python 为什么';这不是简单的搜索工作吗?,python,Python,我只是在一个外部文件(其中包含一个短语)中进行迭代,并想看看是否存在一行(其中包含单词“Dad”),如果我找到它,我想用“Mum”替换它。这是我建立的程序。。。但我不知道为什么它不起作用 message_file = open('test.txt','w') message_file.write('Where\n') message_file.write('is\n') message_file.write('Dad\n') message_file.close() message_temp_

我只是在一个外部文件(其中包含一个短语)中进行迭代,并想看看是否存在一行(其中包含单词“Dad”),如果我找到它,我想用“Mum”替换它。这是我建立的程序。。。但我不知道为什么它不起作用

message_file = open('test.txt','w')
message_file.write('Where\n')
message_file.write('is\n')
message_file.write('Dad\n')
message_file.close()

message_temp_file = open('testTEMP.txt','w')
message_file = open('test.txt','r')

for line in message_file:
    if line == 'Dad':  # look for the word
        message_temp_file.write('Mum')  # replace it with mum in temp file
    else:
        message_temp_file.write(line)  # else, just write the word

message_file.close()
message_temp_file.close()

import os
os.remove('test.txt')
os.rename('testTEMP.txt','test.txt')

这应该很简单…我很生气!谢谢。

你没有任何一行是“爸爸”。您有一行是
“Dad\n”
,但没有
“Dad”
。此外,由于您已经完成了
message\u file.read()
,光标位于文件的末尾,因此
for message\u file
中的行将立即返回
StopIteration
。您应该在
for
循环之前执行
message\u file.seek(0)

print(message_file.read())
message_file.seek(0)
for line in message_file:
    if line.strip() == "Dad":
        ...
这将把光标放回文件的开头,去掉换行符,得到所需的内容

请注意,这个练习是一个很好的例子,说明了如何在一般情况下不做事!更好的实施方式是:

in_ = message_file.read()
out = in_.replace("Dad","Mum")
message_temp_file.write(out)
在这里,您已经阅读了整个文件。 没有留下任何内容供for循环检查

文件对象始终记得上次访问它时停止读/写的位置。 因此,如果调用
print(message_file.readline())
,则会读取并打印文件的第一行。下次调用同一命令时,将读取并打印第二行,依此类推,直到到达文件末尾。通过使用
print(message_file.read())
您已经阅读了整个文件,任何进一步调用
read
readline
都不会给您任何帮助


您可以通过
message\u file.tell()
获取当前位置,并通过
message\u file.seek(value)
将其设置为某个值,或者只需重新打开文件即可。问题很可能是由于您的条件仅与字符串“Dad”匹配,而字符串实际上是“Dad”\n。您可以将条件更新为:

if line == "Dad\n":


最后,在调用print(message_file.read())时,还可以读取整个文件。您或者需要删除该行,或者需要调用message_file.seek(0),以便后面的循环实际执行任何操作。

您所说的。。。它不起作用?您的编辑正在影响给定的答案。您应该解释您所做的编辑,以便将来的读者根据您问题的以前版本理解答案的上下文。
message\u file.read()
读取整个文件,直到它发现EOF(EndOfFile)
message\u file.readline()
将逐行读取文件。这就是for构造的每一轮中发生的情况。它从文件中读取一行,并将其传递到名称行下的循环中。文件对象包含从何处读取文件的位置。因此,如果读取一行,则文件对象已更改。如果你读了整个文件。指针位于文件的和处,每次尝试读取s.th。我的意思是你应该通过编辑答案来解释,而不是在评论中PSure,我会复制它;)@用户3396486很高兴听到这个消息!如果我的答案有助于你找到解决方案,请考虑如果你处理大文件,“更好的实现”会变得复杂,因为你临时把整个文件存储在变量<代码> i<<代码>中。在这种情况下,坚持使用
for
循环比较省钱,因为它一次只存储一行time@MaSp同意,但是由于这个文件已经从他第一次调用
.read()
读取到内存中,所以我无法想象将它保留在内存中是一个问题;)。老实说,更好的实现方法是在迭代行时使用
str.replace
(或
re.replace
),但这需要更多的工作,对于一个如此小的文件来说,也更复杂。
if line == "Dad\n":
if "Dad" in line: