简体   繁体   English

如果字符串在文本文件中并打印行,如何检查Python?

[英]How to check in Python if string is in a text file and print the line?

What I am trying to do is to check whether this string is found in the text file. 我想要做的是检查是否在文本文件中找到此字符串。 If it does, I want it to printout that line, else printout a message. 如果是,我希望它打印出该行,否则打印出一条消息。

I have implemented this code so far: 到目前为止,我已实现此代码:

 def check_string(string):

     w = raw_input("Input the English word: ")
        if w in open('example.txt').read():
            for w.readlines():
                print line
        else:
            print('The translation cannot be found!')

I've tried implementing that but I got a syntax error. 我已经尝试过实现,但是我遇到了语法错误。

It says: 它说:

invalid syntax at the line -- for w.readlines(): 该行的语法无效 - 对于w.readlines():

Any idea on how to go with this line of code? 关于如何使用这行代码的任何想法?

You should try something like this: 你应该尝试这样的事情:

import re
def check_string():
    #no need to pass arguments to function if you're not using them
    w = raw_input("Input the English word: ")

    #open the file using `with` context manager, it'll automatically close the file for you
    with open("example.txt") as f:
        found = False
        for line in f:  #iterate over the file one line at a time(memory efficient)
            if re.search("\b{0}\b".format(w),line):    #if string found is in current line then print it
                print line
                found = True
        if not found:
            print('The translation cannot be found!')

check_string() #now call the function

If you are searching for exact words instead of just sub-string then I would suggest using regex here. 如果您正在搜索确切的单词而不仅仅是子字符串,那么我建议在这里使用regex

Example: 例:

>>> import re
>>> strs = "foo bar spamm"
>>> "spam" in strs        
True
>>> bool(re.search("\b{0}\b".format("spam"),strs))
False

Here is a bit simpler example by using in operator: 通过in运算符中使用in这是一个更简单的示例:

w = raw_input("Input the English word: ") # For Python 3: use input() instead
with open('foo.txt') as f:
    found = False
    for line in f:
        if w in line: # Key line: check if `w` is in the line.
            print(line)
            found = True
    if not found:
        print('The translation cannot be found!')

If you'd like to know the position of the string, then you can use find() instead of in operator. 如果您想知道字符串的位置,那么您可以使用find()而不是in运算符。

暂无
暂无

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

相关问题 如何检查文本文件的每一行是否有字符串并将所有出现该单词的行打印到新文本文件中? - How to check each line of a text file for a string and print all lines with occurrences of that word to a new text file? Python:如何检查文本文件,将其与另一个文本文件中的每一行进行比较,并打印不匹配的行 - Python: How to check a text file, compare it to each line in another text file, and print the lines that do not match 如何从.INI文件中读取字符串,然后从文本文件中读取该字符串并使用Python打印整行? - How to read the string from an .INI file and then read that string from a text file and print the entire line using Python? Python:在文本文件中查找字符串并在同一行中的整数> 1时打印它 - Python: Find a string in a text file and print it if the integer in the same line > 1 Python:逐行解析和打印文本文件 - Python: Parse and Print Text File Line by Line 如何在 Python 中检查在线文本文件中的字符串 - How to check for string in online text file in Python 如何在文本文件中搜索单词并用 Python 打印部分行? - How to search for word in text file and print part of line with Python? 如何在python中使用换行符打印文本文件内容? - How to print text file content with line breaks in python? 如何从文本文件中打印字符串,在 Python 中以新行分隔 - How to print strings from a text file,separated by new line in Python 如何在文本文件中查找一个单词并打印 Python 下一行中的另一个单词 - How to find a word in a text file and print another that is in the next line in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM