← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #153770]: Need to take input from file

 

Question #153770 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/153770

    Status: Open => Answered

RaiMan proposed the following answer:
supposed file content (currently 2 lines):

field name : "Data"
field 2 : "sommoredata".....

--- the script
data = file("absolute path to file")

for line in data.readlines(): # opens file and returns each line
   fields = line.strip().split(':') # see comment
   print fields[0], '=', fields[1]

data.close()

-- comment:
strip() removes the line ending
split(':') returns a list of the substrings separated by the colon

so if each line only contains a key/value pair you could say:

   (key, val) = line.strip().split(':')
   key = key.strip() # to get rid off surrounding whitespace
   val = val.strip() 

or you might collect the things in a dictionary:
myData = {} # place this before the read loop

   (key, val) = line.strip().split(':')
   key = key.strip(); val = val.strip() 
   myData[key] = val

so later on you might access the dictionary

for key in myData.keys():
    print key, '=', myData[key]

many options ;-)

You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.