unicode error when copying Env.getClipboard()

Asked by Josh R

This is a snippet of code that's causing me an error:

description = ""
doubleClick(someScreenshot)
wait(1)
type(Key.RIGHT, KeyModifier.CTRL)
type(Key.RIGHT, KeyModifier.CTRL)
type (Key.END, KeyModifier.SHIFT + KeyModifier.CTRL)
type("c", KeyModifier.CTRL)
description = str(Env.getClipboard())

The objective of this code is to parse an email and copy the information to put into an object. This specific section of code pulls the information out of the user submitted information. One one email a user had the following information:

Apple Gift Card: value £25.00

The script produced the following error due to the '£' symbol:

[error] UnicodeEncodeError ( 'ascii' codec can't encode character u'\xa3' in position 23: ordinal not in range(128) )

I know I need to (dis)allow UTF-8 to get around this, but I can't seem to find a way to make it work with Env.getClipboard. It seems like I need an import of some variety, but I can't seem to make it work, can anyone help resolve this? Thanks.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Josh R
Solved:
Last query:
Last reply:
Revision history for this message
Josh R (joshua-ratliff) said :
#1

I've found a temporary update/solution that resovles the problem, but I'm still looking for more information. I've changed the line to be:

description = str(Env.getClipboard().encode('latin-1'))

Does anyone know what 'latin-1' contains and/or if that's going to be an all encompassing inclusion?

Revision history for this message
Josh R (joshua-ratliff) said :
#2

looks like the encode function can take 'utf-8' as a parameter and that's solved my problem.

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

Yes, that is the reason: what you get from clipboard is a utf-8 encoded string, which is not yet handled correctly with the Jython currently used in Sikuli.

So knowing, that the clipboard might contain non-ASCII characters (usually utf-8), you have to use the encode('utf-8') function.

This might be handled more convenient in a future version of Sikuli (together with a revision of the clipboard handling).

BTW:
description = Env.getClipboard().encode('utf-8')

is sufficient, because Env.getClipboard() already returns a string.