Automatic press enter after type text when reading lines from a file

Asked by Alexandr on 2018-12-13

-------------- solution
use the String::strip():
line = line.strip()

which removes whitespace from beginning and end of string

--------------------------------------------------------------------------------------------------------

Script paste commands(contains in test_2.txt) in "Run"

list_of_anchor_links = open("C:\Source_for_Sikuli_scripts\test_2.txt")
for i in list_of_anchor_links:
    type("r", Key.WIN)
    type(Key.DELETE)
    type (Pattern("1544694065545.png").targetOffset(0,29), i)

But after type first command, automatic press enter
After type second(and next) command - all right

Text in message area:

[log] ( Windows ) TYPE "r"

[log] TYPE "#DEL."

[log] CLICK on L(272,953)@S(0)[0,0 1920x1080] (529 msec)
[log] TYPE "cmd#ENTER."

[log] ( Windows ) TYPE "r"
[log] TYPE "#DEL."

[log] CLICK on L(272,953)@S(0)[0,0 1920x1080] (532 msec)
[log] TYPE "regedit"

Why after TYPE appears #ENTER

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2019-01-09
Last query:
2019-01-09
Last reply:
2018-12-20
RaiMan (raimund-hocke) said : #1

type (Pattern("1544694065545.png").targetOffset(0,29), i)

the second parameter must be a string (a number in your case)

from Python to Java level there is no type check and the bytes representing the number apparently are misinterpreted.

hence:
type (Pattern("1544694065545.png").targetOffset(0,29), str( i))

Alexandr (shamrayonok) said : #2

Not helped...

If I use PASTE instead TYPE, all works right

RaiMan (raimund-hocke) said : #3

Uuuups, sorry - should have read with more concentration :-(

I thought i is a number (implicit convention with for ;-)

... but it is a string.

... and that might be the problem: with type only letters, numbers and some special chars can be used (US keyboard layout)
see faq 933

hence using paste() is the right solution.

Alexandr (shamrayonok) said : #4

I guess automatic press enter happens, because string from outsource txt file, reads with "\n" (newline character) in the end of string.

After type the last string from the list not happens automatic press enter.(in the end of this string there is not "\n")
And if exclude last character(type (i[:-1])) not happens automatic press enter

Best RaiMan (raimund-hocke) said : #5

I am really sorry, to not having seen that - obvious, when reading lines from a file.

Python has the function strip(), which removes whitespace from the beginning and end of a string:

type (Pattern("1544694065545.png").targetOffset(0,29), i.strip())

Alexandr (shamrayonok) said : #6

Thanks!
This Solved My Problem

Alexandr (shamrayonok) said : #7

Thanks RaiMan, that solved my question.