← Back to team overview

sikuli-driver team mailing list archive

[Question #194613]: How to read line by line from a text file and store the content for later use (2)

 

New question #194613 on Sikuli:
https://answers.launchpad.net/sikuli/+question/194613

Good afternoon,

I asked this question approx. 2 mos. ago & raiman was kind enough to answer with a great response.  I need help is why I'm back messing with this.  The original question # was 186633.  Here was my original post & information:

So kinda here's what I'm trying to attempt:
First, obviously, change over to putty then do the type command & execute what is there.
switchApp("putty.exe")
type("sho 123456" + Key.ENTER)
type("sho 234567" + Key.ENTER)
type("sho 345678" + Key.ENTER)
type("sho 456789" + Key.ENTER)
This all works like a champ but what I'm trying to attempt is to take and make it so that instead of having to handtype those id's (& those are just samples for this here, I don't wanna put anything realistic in there). The id's are all of a fixed length so there is no problem there.
What I'm hoping to accomplish is to maybe call a file like a .txt or xls or ... whatever works. Doesn't matter to me as long as I can get the desired results. There are several places I simply have no clue how to write in there so I'll write it in plain text & hopefully someone can either tell me how to do it myself or explain it to me.
switchApp("putty.exe")
(here's where I don't know) make a call to a .txt, xls ... whatever file & call in the id's from that file via, I would assume an array.
like I would say the text file would look like this & be called like ids.txt
123456
234567
345678
456789
I'm looking to try to make these id's be read into the original so you get like:
switchApp("putty.exe")
(input file ids.txt)
sho id1
sho id2
sho id3
and when it was done reading the #'s or even if there was a specified say 20 or 50 id's or whatever the program would end. The array would loop through till it found nothing,
PLEASE ask me any questions you'd like. I'm sure I'm missing pieces so please by all means ask away.
Thanks
RaiMan (raimund-hocke) said on 2012-02-03:  #4  

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.


I'm having trouble as I tried this but can't get past line 5 as I keep receiving the following error on the bottom of sikuli - 

[error] Stopped
[error] An error occurs at line 5
[error] Error message: Traceback (most recent call last):
 File "C:\DOCUME~1\Glenn\LOCALS~1\Temp\sikuli-tmp3773400101687796364.py", line 5, in 
 f = open(ids) # opens ids file
TypeError: coercing to Unicode: need string, 'list' type found

Any help would be appreciated as I'm kinda lost.

Cheers,
Glenn

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