Umlaute in python script

Posted on
Tue Sep 05, 2017 11:26 am
davinci offline

Umlaute in python script

I want to check if a variable contains the text "Mähen".

Code: Select all
variable = indigo.variables[356743265].value
if variable == "Mähen":
...


However, even though it contains "Mähen" it just doesn't work. I think it has to do with encoding...

What can I do?

Posted on
Tue Sep 05, 2017 11:32 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Umlaute in python script

Try:
Code: Select all
if variable == u"Mähen":

Just tested it and it works AFAICS. :)

Posted on
Tue Sep 05, 2017 12:39 pm
davinci offline

Re: Umlaute in python script

Thanks, but this doesn't work for me...

Posted on
Tue Sep 05, 2017 12:52 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Umlaute in python script

Almost:

Code: Select all
if variable.value == u"Mähen":

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Sep 05, 2017 1:39 pm
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Umlaute in python script

Almost, almost! Full test code to avoid confusion: :D
Code: Select all
variable = indigo.variables[356743265].value
if variable == u"Mähen":
  indigo.server.log('TRUE')
else:
  indigo.server.log('FALSE')
Would read better as:
Code: Select all
variableValue = indigo.variables[356743265].value
if variableValue == u"Mähen":
  indigo.server.log('TRUE')
else:
  indigo.server.log('FALSE')
Tested again and it still works for me. :)

Posted on
Wed Sep 06, 2017 1:31 pm
davinci offline

Re: Umlaute in python script

I did the exact same thing but it never works. With another text like "test" it works.


Code: Select all
variableValue = indigo.variables[32452423].value

if variableValue == "test" or variableValue == u"Mähen":
    indigo.server.log(variableValue)

Posted on
Wed Sep 06, 2017 2:32 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Umlaute in python script

I just copy/pasted your script into a new execute script action and only changed the ID of the variable. Logs every time the value is Mähen or test but never for any other value.

Are you sure that your Indigo variable value is exactly the string Mähen and nothing else?

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Sep 06, 2017 11:09 pm
davinci offline

Re: Umlaute in python script

I get the string from html but even when I type manually it ist not working.

The editor is Xcode.

Posted on
Thu Sep 07, 2017 1:52 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Umlaute in python script

davinci wrote:
I get the string from html but even when I type manually it ist not working.

The editor is Xcode.


So it doesn't work if you go to Indigo's variable window and manually change the variable to Mähen?

I am not sure what you mean by The editor is Xcode?

Posted on
Thu Sep 07, 2017 12:05 pm
davinci offline

Re: Umlaute in python script

Yes it doesn't work. If I type "test" it works.

Xcode is the software I use to edit the external python script.

...

Ok it works if I run the script in Indigo. But I prefer using external scripts. :o

Posted on
Thu Sep 07, 2017 12:38 pm
kw123 offline
User avatar
Posts: 8333
Joined: May 12, 2013
Location: Dallas, TX

Re: Umlaute in python script

You might need to add the first line thing that makes it a UTF8 file.

Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-


Sent from my iPhone using Tapatalk

Posted on
Fri Sep 08, 2017 2:53 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Umlaute in python script

I have done some more testing but no solution yet. :|

My new test code is this:
Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#######################
import unicodedata

variableValue = indigo.variables[356743265].value

compareValue = u"Mähen"
compareCharacter = u'ä'

indigo.server.log('variableValue: %s, Type=%s, Length=%s' % (variableValue, type(variableValue), len(variableValue)))
indigo.server.log('compareValue: %s, Type=%s, Length=%s' % (compareValue, type(compareValue), len(compareValue)))

if len(compareCharacter) == 1:
    indigo.server.log('unicodedata for character \'ä\': %s' % (unicodedata.category(compareCharacter)))
elif len(compareCharacter) == 2:
    indigo.server.log('unicodedata for character \'ä\': %s, %s' % (unicodedata.category(compareCharacter[0]), unicodedata.category(compareCharacter[1])))

if variableValue  == compareValue:
  indigo.server.log('TRUE = %s' % variableValue)
else:
  indigo.server.log('FALSE = %s' % variableValue)
If I run this as an embedded script, I get:
Script variableValue: Mähen, Type=<type 'unicode'>, Length=5
Script compareValue: Mähen, Type=<type 'unicode'>, Length=5
Script unicodedata for character 'ä': Ll
Script TRUE = Mähen

If I run it as a file script, I get:
Script variableValue: Mähen, Type=<type 'unicode'>, Length=5
Script compareValue: Mähen, Type=<type 'unicode'>, Length=6
Script unicodedata for character 'ä': Lu, Sc
Script FALSE = Mähen

It seems to be something to do with the unicode encoding being different between the two ways of running the script. The file script is producing two bytes (i.e. ä) for the single character ä.

Needs a unicode expert to resolve. I suspect it might be simply done. :wink:

Posted on
Fri Sep 08, 2017 6:31 am
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: Umlaute in python script

Interesting. The way Indigo runs both embedded and linked/file scripts is special since it is having to import and run them in the context of the plugin host (so the indigo.* namespace APIs are available). Let me look into a couple of things and see if I can figure out what is going wrong.

Image

Posted on
Fri Sep 08, 2017 9:40 am
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: Umlaute in python script

I found the problem. Next version of Indigo will properly import and execute external python UTF8 encoded script files.

Image

Posted on
Fri Sep 08, 2017 10:13 am
davinci offline

Re: Umlaute in python script

Thanks for that.

Who is online

Users browsing this forum: No registered users and 4 guests

cron