简体   繁体   English

在文本文件中搜索特定单词

[英]Search for specific word in text file

I am currently trying to search for a specific word in a text file.我目前正在尝试在文本文件中搜索特定单词。 I've already wrote my code but it seems that the script is not working correctly.我已经编写了我的代码,但脚本似乎无法正常工作。

My code:我的代码:

main_file = open('myfile.txt','w')
        

x = 'Hello'
x_main = main_file.write(x)
with open('myfile.txt') as f:
    datafile = f.readlines()
for line in datafile:
    if 'Hello' in line: #I also tried Hello without the quotes
        print("ok")
print(x)

I get only Hello as an output and not ok + Hello.我只得到 Hello 作为 output 而不是 ok + Hello。

I hope that someone can help me out with this little problem:)我希望有人能帮我解决这个小问题:)

Thank's for every help and suggestion in advance:)感谢您提前提供的所有帮助和建议:)

Your main problem is that you don't close the file after your file after writing to it.您的主要问题是您在写入文件后没有关闭文件。 Like you have done to open your file with open(file) as file you can do the same in order to read the file.就像您with open(file) as file您可以执行相同的操作来读取文件。 This way you avoid the hastle of writing file.close() .这样您就可以避免编写file.close()的麻烦。

Other than that, your code seems fine.除此之外,您的代码似乎还不错。

with open('test.txt','w') as f:
    x = 'Hello'
    f.write(x)
with open('test.txt') as f:
    datafile = f.readlines()
for line in datafile:
    if 'Hello' in line: #I also tried Hello without the quotes
        print("ok")
print(x)

Try this one试试这个

main_file = open('myfile.txt','w')
    
x = 'Hello'
x_main = main_file.write(x)
main_file.close()
with open('myfile.txt', 'r') as f:
   datafile = f.readlines()
for line in datafile:
   if 'Hello' in line: #I also tried Hello without the quotes
      print("ok")
print(x)    

When writing to your file, you never closed the file ( file.close() ).写入文件时,您永远不会关闭文件 ( file.close() )。 I would suggest using the open() context manager so you don't have to worry about writing file.close() .我建议使用open()上下文管理器,这样您就不必担心编写file.close()

string = "Hello"
with open("myfile.txt", "w") as file:
    file.write(string)

with open("myfile.txt") as file:
    lines = file.readlines()

for line in lines:
    if "Hello" in line:
        print("OK")

print(string)

I am currently trying to search for a specific word in a text file.我目前正在尝试在文本文件中搜索特定单词。 I've already wrote my code but it seems that the script is not working correctly.我已经编写了代码,但脚本似乎无法正常工作。

My code:我的代码:

main_file = open('myfile.txt','w')
        

x = 'Hello'
x_main = main_file.write(x)
with open('myfile.txt') as f:
    datafile = f.readlines()
for line in datafile:
    if 'Hello' in line: #I also tried Hello without the quotes
        print("ok")
print(x)

I get only Hello as an output and not ok + Hello.我只得到一个 output 的 Hello 而不是 ok + Hello。

I hope that someone can help me out with this little problem:)我希望有人可以帮助我解决这个小问题:)

Thank's for every help and suggestion in advance:)提前感谢您的每一个帮助和建议:)

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

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