sikuli-driver team mailing list archive
-
sikuli-driver team
-
Mailing list archive
-
Message #21577
Re: [Question #236949]: How to compare the string from two different text file?
Question #236949 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/236949
Status: Open => Answered
RaiMan proposed the following answer:
--- first
this is my optimized solution with some comments
import os
# finds and returns the name in a file
def getName(file):
f = open(file)
name = ""
for line in f.readlines():
# if line does not contain the token read next one
if line.count("Student Name") == 0: continue
# we split the line at the hyphen
# the trailing part goes to name
(head, name) = line.split("-")
# we strip the leading/trailing whitespace
name = name.strip()
# found, so we can leave
break
f.close()
return name
# compares the names in 2 files, prints a log
# and returns the equal name or None if not equal
def compareNames(file1, file2):
name1 = getName(file1)
name2 = getName(file2)
if name1 == name2:
print "we have a match:", name1, "in:", file1, "and:", file2
return name1
else:
print "different names:", name1, name2, "in:", file1, "and:", file2
return None
# the basedir for the files
dir = "/Users/rhocke/Desktop/Sikuli/koventan"
# creates the file names
file1 = os.path.join(dir, "file1.txt")
file2 = os.path.join(dir, "file2.txt")
file3 = os.path.join(dir, "file3.txt")
result = compareNames(file1, file2)
print "returns:", result
# one could now decide how to proceed
if not result:
print "proceed on no match"
else:
print "proceed on match"
result = compareNames(file1, file3)
print "returns:", result
# one could now decide how to proceed
if not result:
print "proceed on no match"
else:
print "proceed on match"
and produces:
different names: Kumar Siva in: /Users/rhocke/Desktop/Sikuli/koventan/file1.txt and: /Users/rhocke/Desktop/Sikuli/koventan/file2.txt
returns: None
proceed on no match
we have a match: Kumar in: /Users/rhocke/Desktop/Sikuli/koventan/file1.txt and: /Users/rhocke/Desktop/Sikuli/koventan/file3.txt
returns: Kumar
proceed on match
file3 has same content as file1
--- second on comment #10
after
line1 = open (my_dir+"file1.txt").readlines()
line1 is a list (array) of the contained lines including line breaks.
... but re.search() needs a string (means one line in this case)
You might integrate the usage of RegEx's into my solution (looks more
professional ;-)
My solution has the advantage, that the key functions are packed in
def()s, so you can concentrate on the workflow.
--
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.