简体   繁体   中英

How to find more words in a file at once?

In my program I am reading another python program and I want to find some special python keywords. When I find them I want to print out some code in front of that keyword found and after it.

This is my code:

if line.find("def") == True or line.find("if") == True or line.find("elif") == True:
   print("<span style=\"color: orange;\">",True,"</span>","<br>", end="") 

In the print statement "True" I want it to be the value that was read by the program.

I don't know if I am doing the "or" statement right but the output is not working. I don't get any error or anything . I expect my output to be the HTML code above and then the keyword found and the closing HTML code.

Example Output:

<span style=\"color: orange;\">if</span>
<span style=\"color: orange;\">def</span> 
<span style=\"color: orange;\">elif</span>

As you can see by looking up str.find in the docs, or just using the built-in help, it doesn't return true or false, it returns the index the substring was found at, or -1 if not found.

So, line.find("def") == True will always be false. Whether it was found at column 10 or column 70 or not at all, none of 10, 70, or -1 are equal to True. What you want is line.find("def") != -1 .

However. there's an even better way: if you don't care about the index, just check "def" in line :

if "def" in line or "if" in line or "elif" in line:

Or, maybe even better:

if any(keyword in line for keyword in "def", "if", "elif"):

By the way, even for functions that do return True or False , you almost never want to check if x == True . Basically only when there's multiple possible truthy values, and you need to distinguish True from the other ones. Otherwise, just check if x .


But you're still not going to get the output you want from the print you wrote:

print("<span style=\"color: orange;\">",True,"</span>","<br>", end="") 

You're printing the constant True ; there's no way that could ever print some other string like def , it's always just going to print True .

If you need to know which one matched, you can't just throw all the conditions together, you have to test each one separately, as the other answers show.

Or… what you're doing here is building a very simple parser. You might want to consider learning how to use PEG or pyparsing or something that will make this easier and less tedious. Or, if the text file really is (legal) Python source code, the Python source code parser that comes built in with the Python stdlib (the ast module). Or, for something this simple, just regular expressions:

m = re.search(r'def|if|elif', line)
if m:
    print("<span style=\"color: orange;\">", m.group(), "</span>","<br>", end="") 

I would recommend not using or in this case and rather breaking this up into if and elif statements to preserve what keyword was found. Store that in a variable (in this case value ) and use that in the print statement.

if line.find("def") != -1:
    value = "def"
elif line.find("if") != -1:
    value = "if"
elif line.find("elif") != -1:
    value = "elif"
print("<span style=\"color: orange;\">", value,"</span>","<br>", end="") 

Try this:

checklist = ['def', 'if', 'elif']
for x in checklist:
    if x in line:
        print("<span style=\"color: orange;\">",x,"</span>","<br>") 

EDIT:

try this for the case you described below

import re
re.compile(r'(((if|elif|else|in)\s*?)+)', re.I).sub(r'<span style=\"color: orange;\">\1</span><br>', line)

ONE MORE EDIT:

Try this then:

checklist = ['def', 'if', 'elif']
array = line.split(' ')
output = []
for x in array:
    if x in checklist:
        output.append("<span style=\"color: orange;\">%s</span><br>" % x)
    else:
        output.append(x)
print(' '.join(output))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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