How to read line by line from a text file and store the content for later use (2)

Asked by Glenn Diaz

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

Question information

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

-- but can't get past line 5
But you are not working with the suggested code, since the error says:
f = open(ids) # opens ids file

ids must be a valid filename given as a string.

So paste your relevant code completely.

Revision history for this message
Glenn Diaz (diazg) said :
#2

ummm sorry but I'm not sure what you're requesting of me. Are you asking for the info within ids.txt? If so here's how I have it.

123456
234567
345678
456789

and that's in the utf-8 code as you had suggested previously. I'm hoping that's what you're looking for, if not please let me know & I'll give you whatever other relevant info necessary.

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

No, I want to see your SCRIPT, where you get the error.

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

this is still the basic script template to read something line by line in a list for later use:

I have used other variable names, to make it clearer

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

# Now you can add your putty loop
switchApp("putty.exe")
for id in idsList:
    type("sho " + id + Key.ENTER)

Revision history for this message
Glenn Diaz (diazg) said :
#5

I think I figured it out. I kinda stepped back a bit because the biggest issue I'm running into is having to key in manually all of the id's into the ids.txt file. I was trying to do a simple cut & paste but that's not really possible with everything I've come up against so far. So I simplified & that seems to be working.

Thank you for all the help!!!!

Cheers,
Glenn