← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #668564]: how can I split a string

 

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

Roman Podolyan posted a new comment:
Hello Marc.

Your initial problem was that string is needed for string.split to work, and Match type does not convert automatically.
To get string from Match (or any other type) you need str(Match).

Here piece of code working in Sikuli 1.1.1:
____
find("1525525190673.png")
string_result = str(getLastMatch())
array_of_split = string_result.split()
print(array_of_split)
____

But in my opinion this way is not that good for code readability sake.
I had a similar need to extract part of a string recently. Here what I came with: 

____
my_string=  "Alpha: 0.1674444 Beta: 0.221314"
alpha_string = my_string[my_string.find("Alpha: ") + 7 : my_string.find(" Beta: ")]
print(alpha_string)

# this is working code producing output 0.1674444  in SikuliX 1.1.1
____ 

Two techniques used here:
1) Python slicing — using some_string[x:y] to get part of string between integer positions x (included) and y (not included)
2) string .find method allowing to find first inclusion of some substring in some big string. 

The second piece of code informs that you actually need slice of string
between "Alpha:" + some offsent and " Beta: ", what is IMO more
convenient.

Hope that helps.

-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.