← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #186633]: Array question

 

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

    Status: Open => Answered

RaiMan proposed the following answer:
ok, good explanation.

This is what I understand:
You have a list of ids, that you want to loop through. To be flexible, you want to have them in some text file.
This is read in the script and the ids are stored in a list (this is the Python word for an array).

Set up a text file e.g. ids.txt and fill in the id's with some editor.

Then make a Sikuli script:

import os
ids = [] # empty list/array
dir = "c:/folder/subfolder" # the folder, where your ids.txt is
fileName = os.path.join(dir, "ids.txt") # makes a Windows filename
f = open(filename) # opens ids file
for line in f.readlines(): # reads line by line
    id = line.strip() # get rid of newline
    print id
    ids.append(id) # add id to list/array
f.close() # close file

# now you have all your ids in the list/array ids
for i in range(len(ids)):
     print "this is id %d: %s"%(i+1, ids[i])

comment on the filename:
to avoid problems with backslashes we use the above feature os.path.join, that concatenates folders (where you can have / for a \) with a file name and converts it internally in a valid Windows filename.

# or whatever you want to do with ids

take care for Python's indentation: faq 1800

and something on loops: faq 1437

Have fun. Hope it helps.

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