Is it possible to join a multiline string using python regex in Sikuli??

Asked by eduardobedoya

In sikuli I've get a multiline string from clipboard like this...
Names = App.getClipboard();
Name =
#corazona
#Pebleo00
#cofriasd
«paflio

and I have use this regex to delete the first character
if it is not in x00-x7f hex range or is not a word, or is a digit
import re
Names = re.sub(r"(?m)^([^\x00-\x7F]+|\W|\d)", "", Names)
So now...
Names =
corazona
Pebleo00
cofriasd
paflio

I am having trouble with the second regex that convert "Names" into a kinda List
I would like to convert "Names" into...
'corazona', 'Pebleo00', 'cofriasd', 'paflio',

So sikuli can then recognize it as a List by using...
NamesAsList = eval(Names)
in order to get a proper List to work with

How could I do this in python? is it necessary to use regex, or there is other way to do this in python?

I have already done this but using .Net regex, I just don't know how to do it in python, I have googled it with no result.
This is how I did it using .Net regex
Text to find:
(.*[^$])(\r\n|\z)
Replace with:
'$1',%" "%

Thanks Advanced.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
eduardobedoya
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
Eugene S (shragovich) said :
#1

All standard Python functionality will work in Sikuli as well.

Your question is purely Python. Refer to Python documentation.

Revision history for this message
eduardobedoya (gonnabdh) said :
#2

Thanks Eugene S, that solved my question.

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

some Python tutorial ;-)

Names = """
 Names =
   #name1
   #name2
  ~name3
"""
namelist = Names.split()[2:]
for i in range(len(namelist)):
   namelist[i] = namelist[i][1:]
print namelist

printout:
['name1', 'name2', 'name3']

Revision history for this message
eduardobedoya (gonnabdh) said :
#7

Thanks Rainman!
It worked, I just would like to understand why the """ """and the Name = (inside another Name)
please could you suggest me some related tutos?

Thanks Again man.

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

name = """
some text
some text
"""

is the so called "here text" (available in most scripting languages), that allows to easily define a multiline text inline in the code.
mostly used for initialisation and testing (instead of using some file or other more complex things)

So in your case I simply simulated what I supposed you get from clipboard.

Revision history for this message
eduardobedoya (gonnabdh) said :
#11

But RaiMan

you are setting

Names ='''
name1
name2
name3
'''
an then with simply using...
namelist = names.split()[0:]

printout:
['name1', 'name2', 'name3']

shure, it works fine, but....

In my case, I receive the name1, name2, name3 from clipboard, and so I use
Names = App.getClipboard();
in order to store all names in Names variable
but doing so, I don't know why, my Names variable is different than the Names variable you are setting

your Names var
Names ='''name1,
name2,
name3
'''
if you print them they seem the same, but when doing the split both have different results

So when I use
namelist = names.split()[0:]
I get...
[u'name1', u'name2', u'name3']

How I can get a variable like the one that you set?

I guess the variable I have is not a multine variable, but a string with many carriage returns

Also I have tried using regex in order to simply remove the \r\n from my Names variable
newNames = re.sub(r"\r\n", " ", Names)
why is that???

please tell me, how can I remove the \r\n (new lines) form my clipboard,
Iam stuck with this

THanks Advanced.

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

it is exactly what you need.

As often with your questions:
a look into Python language guide would have told you, that something looking like
u'some text' is a unicode string encoded in utf-8 (which is what you get from clipboard).

so simply use it as a list of strings.

this is only the way, how print tells you, that the strings are unicode strings.

Revision history for this message
eduardobedoya (gonnabdh) said :
#15

I would like to convert "Names" into...
'corazona', 'Pebleo00', 'cofriasd', 'paflio',

cuz there is another list that I need to compere it to...
Names2 = ['corazona', 'paflio']
using...
NamesSelected = [i for i, v in enumerate(Names) if v not in Names2]

if I compare the clipboard as it is, I get an unespected result,
How could I convert this unicode string enconced in utf-8 into
'corazona', 'Pebleo00', 'cofriasd', 'paflio',

Thanks Advanced.

Revision history for this message
eduardobedoya (gonnabdh) said :
#19

solved using this...
s= u'£10'
s.encode('utf8')

I know its said taht the "u" is purely used to let you know it's a unicode string in the representation, that it will not affect the string itself, that unicode strings work exactly as normal strings. But this is not the case in Sikuli xD
I could not leave it as unicode string because of the reason explained in my comment above.
There is an special python manual for Sikuli purposes xD

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

I made a faq, that should explain some of your doubts:
faq 2711

If you are sure, that what you get from clipboard only contains ascii characters, the this is the easiest approach:

Names = str(App.getClipboard());

then you could just forget about unicode in the following workflow.

If you have to be aware of unicode from the clipboard, then you simply have to take care, that all variables are unicode afterwards.

Names = App.getClipboard();
# ... convert Names to a list
Names2 = [u'corazona', u'paflio']
NamesSelected = [i for i, v in enumerate(Names) if v not in Names2]