We want to display logged in user on machine HMI. User name is retrieved from database and includes national characters. However, HMI accepts only ASCII symbols. How can I replace national symbols in Ignition string type tag with ASCII symbols.
Something like this works in online Python interpreter but not in Ignition script console (the same string containing national characters is returned for output string)
# create a dictionary with national characters and their ASCII equivalents
translation_dict = {
'Ą': 'A',
'ą': 'a',
'Č': 'C',
'č': 'c',
'Ę': 'E',
'ę': 'e',
'Ė': 'E',
'ė': 'e',
'Į': 'I',
'į': 'i',
'Š': 'S',
'š': 's',
'Ų': 'U',
'ų': 'u',
'Ū': 'U',
'ū': 'u',
'Ž': 'Z',
'ž': 'z'
}
# input string with national characters
#TagPath = "[default]IDBadgeManager/AccessControl/TestString"
#input_string = system.tag.readBlocking(TagPath)[0].value
input_string = "ĄČęėį"
# replace national characters with ASCII symbols using translation_dict
output_string = ''
for sym in input_string:
if sym in translation_dict:
output_string += translation_dict[sym]
else:
output_string += sym
# print output
print(input_string)
print(output_string)
this should probably handle most letters
import unicodedata
newValue = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
1 Like
EDIT: forget everything I said below, with the exception of unicode being problematic. Use unicodedata
unicodedata seems to have issues. Sample string returns AAAAA.
import unicodedata
input_string = unicode("ĄČęėį")
output_string = unicodedata.normalize('NFKD', input_string).encode('ascii', 'ignore')
Unidecode require Python 3.5.
Unicode is sometimes... problematic in Jython. Use a u in front of literals, and unicode() for assigning.
This is your code, modified for posterity.
# create a dictionary with national characters and their ASCII equivalents
translation_dict = {
u'Ą': 'A',
u'ą': 'a',
u'Č': 'C',
u'č': 'c',
u'Ę': 'E',
u'ę': 'e',
u'Ė': 'E',
u'ė': 'e',
u'Į': 'I',
u'į': 'i',
u'Š': 'S',
u'š': 's',
u'Ų': 'U',
u'ų': 'u',
u'Ū': 'U',
u'ū': 'u',
u'Ž': 'Z',
u'ž': 'z'
}
# if you need to do multiple lookups, assign the keys to a variable.
keys = translation_dict.keys()
# input string with national characters
#TagPath = "[default]IDBadgeManager/AccessControl/TestString"
#input_string = system.tag.readBlocking(TagPath)[0].value
input_string = unicode("ĄČęėį")
# replace national characters with ASCII symbols using translation_dict
output_string = ''
for sym in input_string:
if sym in keys:
output_string += translation_dict[sym]
else:
output_string += sym
# print output
print(input_string)
print(output_string)
2 Likes
Apologies, forgot to use my own advice in my reply, lol
1 Like