[Tutor] find() doesnt work as expected

spir denis.spir at free.fr
Sat Jan 2 11:00:50 CET 2010


MK dixit:

> At first, happy new year to all of you.
> 
> I am trying to write a little script which uses one file
> as input and looks if the string from this file are in target file. And
> if not prints out that line/string so that i know which strings must
> be added to complete the target file. The format of the target file
> is different as the source/string query file.
> 
> I tried this but it gives nothing out:
> 
> # -------------------------
> rom optparse import OptionParser
> import os
> #import re
> 
> # ------------------------------
> 
> def help_here():
> 	print """
> 	I need the input file and the file to check.
> 	Both files must exist. Try --help.	
> 	"""
> 	exit()
> 	
> # ------------------------------	
> parser = OptionParser()
> parser.add_option("-i", "--input-file", type="string",
> 				dest="input_file",
> 				help="input file with querys to check")
> parser.add_option("-f", "--file-to-check", type="string",
> 				dest="file_to_check",
> 				help="file which will be checked with querys from input file")
> 				
> (options, args) = parser.parse_args()
> input_file=options.input_file			
> file_to_check=options.file_to_check	
> 
> infi = open(input_file,"r")
> ftck = open(file_to_check,"r")
> ftck_datei=ftck.read()
> ftck.close()
> 
> for a in infi:
> 	found = ftck_datei.find(a)
> 	if found==-1:
> 		print a
> 
> infi.close()
[...]
> The thing is that the target file could be a XML file or something so
> that it looks if the query is in the line of the targetfile.
> Else i could use diff but my files are not the exact text but have
> strings in it which i want to query.
> 
> Thank you.

I don't know exactly why your version does not work. Just 3 side notes:
1. Your naming is IMO rather confusing. For instance, "input_file" is actually a _name_, not a object that represents a file. Calling "infi" a file that actually holds reference data while another file is your real input also makes your code hard to follow.
2. Your test data does hold any missing expression!
3. You don't need the find method to check a substring existence, if the actual position is not needed: use the 'in' operator instead.

I first thought that you forgot / did not know that file.readline(s) (implicitely called by "for a in infi") keeps newlines. But your test case should work anyway. But a real case with XML data would probably not. Maybe your real test case is not the one you published?
See http://docs.python.org/library/stdtypes.html#file-objects for details.

Below a version that works with text input instead of files (added Test* expressions):

expressions = reference.split('\n')
for expression in expressions:
#   print (expression in input)		# debug
   if expression not in input:
      print expression
==>:
Test1
Test2
Test3


Denis
________________________________

la vita e estrany

http://spir.wikidot.com/


More information about the Tutor mailing list