Search inside files and get particular data

Asked by Jeff Sant

Dear Friends,

I have to extract data from a text file (studentinfo.txt), the format of the text file is as following

student_name father_name DOB

Eg. records are

James Smith 1992/12/03
Paul Adams 1977/05/02

I want to get the first and the last name from the file and store it to variable

e.g

var firstname = find firstname
var lastname = find lastname

And then save it to another file.

I can not find a way how to do it, any help is appreciated.

Thanks

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

inFile = open("absolute-path-to-studentinfo.txt")
content = inFile.readlines() # now contains all lines in a list
inFile.close()

# I am not really sure, what you want to write to the other file

outFile = open("absolute-path-to-studentinfo_out.txt", "w")
for line in content:
    fields = line.strip().split()
    firstname = fields[0]
    lastname = fields[1]
    print("firstname = %s, lastname = %s" % (firstname, lastname))
    outFile.write("firstname = %s, lastname = %s\n" % (firstname, lastname))
outFile.close()

look question https://answers.launchpad.net/sikuli/+question/169128 for info links

Revision history for this message
Jeff Sant (sant-jeff) said :
#2

Dear Friend,

The code you gave works perfect, and i have also visited the link you have shared.

But i can not find a way to cut data from one file and paste it to another, is there any way to do it?

Thanks

Revision history for this message
RaiMan (raimund-hocke) said :
#3

I do not understand, what you mean with "way to cut data from one file and paste it to another".

the above solution creates a new file, that contains what ever you want.

Revision history for this message
Jeff Sant (sant-jeff) said :
#4

Dear Friend,

I want to take input from a file eg. testcases.text

testcases = open("c:\testcases.txt")
pass = open("c:\pass.txt")
fail = open("c:\fail.txt")

testcases.readline()

if test is pass then do the following:
1. remove the case from the testcases file
2. append the test case into pass file

if test is faile then do the following:
1. remove the case from the testcase file
2. append the test case into fail file

How can i do this, as i am trying to append using open("anyfile", 'a') but it is not saving it to the file.

Please help me regarding this.

Thanks

Revision history for this message
Enix Shen (enix12enix) said :
#5

As my understanding, this is not related with sikuli. Please see here http://docs.python.org/library/stdtypes.html#file-objects

Revision history for this message
RaiMan (raimund-hocke) said :
#6

Ok, this is understandable ;-)

a rough template (details about file handling see Enix's comment above):

testcasesFile = open(r"c:\testcases.txt")
pass = open(r"c:\pass.txt", "a")
fail = open(r"c:\fail.txt", "a")

testcases = testcasesFile.readlines()
testcasesFile.close()
for line in testcases:
    case = line.strip() # get rid of linefeed
    # analyze case
    # and run your test
    # a test should return True (pass) or False (failed)
    result = runTest(case)
    if result: pass.write(line)
    else: fail.write(line)

pass.close()
fail.close()

This does not contain "remove the case from the test cases file", because in your workflow all tests are processed and pass or fail.
So removing the tests is the same as emptying the file testcases, which could be done at the end:

testcasesFile = open("c:\testcases.txt", "w")
testcasesFile.close()

Revision history for this message
Jeff Sant (sant-jeff) said :
#7

Dear Raiman,

Thanks for the update, actually i want to remove it for the safe side, as if the execution stops either maually or due to any error, it will start working on all the test cases, that i dont want. So i want to remove cases so it can start from where it left.

Hope you understand.

Thanks

Revision history for this message
RaiMan (raimund-hocke) said :
#8

ok, understood.

-- updated template:

testcasesFileName = r"c:\testcases.txt"
testcasesFile = open(testcasesFileName)
pass = open(r"c:\pass.txt", "a")
fail = open(r"c:\fail.txt", "a")

testcases = testcasesFile.readlines()
testcasesNotDone = testcasesFile.readlines()
testcasesFile.close()
for line in testcases:
    case = line.strip() # get rid of linefeed
    # analyze case
    # and run your test
    # a test should return True (pass) or False (failed)
    try:
       result = runTest(case)
       testcasesNotDone.pop(0) # delete the processed entry from the list
       if result: pass.write(line)
       else: fail.write(line)
    except:
       continue

testcasesFile = open(testcasesFileName, "w")
for line in testcasesNotDone:
    testcasesFile.write(line)
testcasesFile.close()
pass.close()
fail.close()

now, if anything happens in your "runTest(case)", that would normally end the script with an exception, we skip to the next test.
At the end the test cases, that could not be processed are written back to the file.

Be aware, that this is only protected against exceptions, that occur inside "runTest(case)".

Revision history for this message
Jeff Sant (sant-jeff) said :
#9

Dear Friend,

This works great, and goes from line one and two and etc...

But i want to get line 1 and 2 together, and then 3 and 4 together.

How can i do that?

Thanks

Revision history for this message
RaiMan (raimund-hocke) said :
#10

If I understand right, then 2 lines in the input file together represent a test case.

I have corrected a bug with the handling of testcasesNotDone

-- updated template:

testcasesFileName = r"c:\testcases.txt"
testcasesFile = open(testcasesFileName)
pass = open(r"c:\pass.txt", "a")
fail = open(r"c:\fail.txt", "a")

testcases = testcasesFile.readlines()
testcasesNotDone = testcasesFile.readlines()
testcasesFile.close()
notDone = 0
for i in range(0, len(testcases), 2):
    case1 = line[i].strip() # case's first line
    case2 = line[i+1].strip() # case's second line
    # analyze case
    # and run your test
    # a test should return True (pass) or False (failed)
    try:
       result = runTest(case1, case2)
       testcasesNotDone.pop(notDone) # delete the processed entry from the list
       testcasesNotDone.pop(notDone+1) # delete the processed entry from the list
       if result: pass.write(line[i]+line[i+1])
       else: fail.write(line[i]+line[i+1])
    except:
       notDone += 2
       continue

testcasesFile = open(testcasesFileName, "w")
for line in testcasesNotDone:
    testcasesFile.write(line)
testcasesFile.close()
pass.close()
fail.close()

Revision history for this message
Jeff Sant (sant-jeff) said :
#11

Dear Friend,

Geting this error

 case1 = line[i].strip() # case's first line NameError: name 'line' is not defined

When i try to use the for line in testcases: then it starts to get each character instead of each line.

Thanks

Revision history for this message
RaiMan (raimund-hocke) said :
#12

Sorry, my fault.

inside this for loop

for i in range(0, len(testcases), 2):

you have to replace the word line with testcases

-- comment:

for line in testcases: steps through testcases one line at a time

but you wanted to step through with 2 lines at a time. so we have to use for i in range(): which steps through the index. i contains the index inside the loop, which has to be used to select the appropriate entry in testcases.

Can you help with this problem?

Provide an answer of your own, or ask Jeff Sant for more information if necessary.

To post a message you must log in.