Grepping words for match in a file

DL Neil PythonList at DancesWithMice.info
Sat Dec 28 21:54:22 EST 2019


On 29/12/19 5:14 AM, Dan Sommers wrote:
> On 12/28/19 12:29 AM, Mahmood Naderan via Python-list wrote:
>> Hi
>> I have some lines in a text file like
>> ADD R1, R2
>> ADD3 R4, R5, R6
>> ADD.MOV R1, R2, [0x10]
>> If I grep words with this code
>> for line in fp:
>>      if my_word in line:
>> Then if my_word is "ADD", I get 3 matches. However, if I grep word 
>> with this code
>> for line in fp:
>>      for word in line.split():
>>          if my_word == word:
>> Then I get only one match which is ADD R1. R2.
>> Actually I want to get 2 matches. ADD R1, R2 and ADD.MOV R1, R2, 
>> [0x10] because these two lines are actually "ADD" instructions. 
>> However, "ADD3" is something else.
>> How can I fix the code for that purpose?
> (1) word.startswith() won't solve your problem.  Expliticly
> checking the character after the second "D" would.  You'll
> have to determine which characters are or aren't part of
> the instruction.  A complete solution probably depends on
> what else you are or will look for in the future.
> 
> (2) That looks like a programming language (88000? POWER?).
> Watch out for comments containing the word ADD, too.


For which reason, and given its finite and limited nature, I'd prefer to 
be explicit:-

Assuming "ADD" is only a sample of the total task, perhaps it will not 
be too impractical to create categories of instruction:

	ADD_CATEGORY = [ "ADD", "ADD.MOV" ]
	etc

Now, you can perform an "in" on the first token:

	if line.split()[ 0 ] in ADD_CATEGORY:
		# this command one of those in the ADD category...

In this way, rather than relying upon (actually *not* being able to rely 
upon) similarities of appearance, you have complete control to limit 
which commands fit into which category/ies...
-- 
Regards =dn


More information about the Python-list mailing list