How can I supress/eliminate "u' " in lists, etc

Sorry for my silly question, but when try to get the names of items of a group with these commands

listOfMembers2a = [item.name for item in ir.getItem("gGTags").members if item.state == OnOffType.OFF]
LOG.info("List of Member 2a :{} ".format(listOfMembers2a))

listOfMembers3 = map(lambda item: item.name, filter(lambda item: item.state == OFF, ir.getItem("gGTags").members))
LOG.info("List of Mem 3-Map :{} ".format(listOfMembers3))

listOfMembers4 = [item.name for item in ir.getItem("gGTags").members if item.state > DecimalType(25)]
LOG.info("List of Members 4 :{} \n ----------------------------Test" \
         "-Zeilenumbruch im logger und Editor------------------------{}".format(listOfMembers4,listOfMembers3)) # first parameter "listOfMembers4" - newline  "\n" for logger - "\" newline for Editor - second parameter "listOfMembers3"
#LOG.info(listOfMembers4)
listOfMembers5 = [item.name for item in ir.getItem("gGTags").members if item.state > QuantityType(u"25 °C")]
LOG.info("List of Members 5 :{} ".format(listOfMembers5))

listOfMembers6 = [item.name for item in ir.getItem("gGTags").members]
LOG.info("List of Members 6 :{} ".format(listOfMembers6))

it results in

2020-03-18 23:41:20.666 [INFO ] [jsr223.TEST                         ] - List of Members 2 :[u'V10', u'test'] 
2020-03-18 23:41:20.675 [INFO ] [jsr223.TEST                         ] - List of Member 2a :[u'V10', u'test'] 
2020-03-18 23:41:20.684 [INFO ] [jsr223.TEST                         ] - List of Mem 3-Map :[u'V10', u'test'] 
2020-03-18 23:41:20.702 [INFO ] [jsr223.TEST                         ] - List of Members 4 :[u'GTag_1', u'GTag_2', u'V10', u'V10_1', u'test'] 
 ----------------------------Test-Zeilenumbruch im logger und Editor------------------------[u'V10', u'test']
2020-03-18 23:41:20.719 [INFO ] [jsr223.TEST                         ] - List of Members 5 :[] 
2020-03-18 23:41:20.728 [INFO ] [jsr223.TEST                         ] - List of Members 6 :[u'GTag_1', u'GTag_2', u'V10', u'V10_1', u'test'] 

such a list, where the names are shown, but before the name there is always a u’.

How can I get rid of it ?

I think in Python the u denotes a Unicode string.I do not know if you can suppress that easily. @5iver might know.

1 Like

Thx for reply, but I don’t understand what for it is :thinking:

If I’m using this code:

listOfMembers2a = [item.name for item in ir.getItem("gGTags").members if item.state == OnOffType.OFF]
LOG.info("List of Member 2a :{} ".format(listOfMembers2a[1]))

with index, I get a log without u

like here 

```csv
2020-03-19 14:24:08.530 [INFO ] [jsr223.TEST    ] - List of Member 2a :test 

I have not done much with python or jsr223 before so take this with a pinch of salt.

Have you tried “reducing” the list into a single string before logging?
i.e. [“thing1”, “thing2”, “thing3”] ⇒ “thing1, thing2, thing3”
From a quick search it looks like it would be something like this in python:

import functools

myListOfMembers = ["thing1", "thing2", "thing3"]

myListAsString = functools.reduce(lambda a,b: a + ", " + b, myListOfMembers)

LOG.info("List of Member :{} ".format(myListAsString))
1 Like

It declares the string to be of unicode type (which you need for umlauts etc).

Don’t. Your output is just debug output anyway. When you use print or any other mechanism, it won’t be in there. If you really need to, you can convert it using str(....), but be aware of the consequences.

It’s not easy to explain the whole background.
Python 2 per default uses ASCII while Python 3 switched to Unicode.
Google for Python 2 (not 3 ! jython used in OH is Python 2) and “unicode” and read up on that.

2 Likes

Thx to you all for your hints. I will google for python2 and unicode.
My intension was only to have a “nice” printout of the list in the logger, not to change the sense (type of the values). So I will test with strg.

In my example above with an indexed value of the list it works as expected, without u’.
So for now I will close the post and learn more about python/jython/jsr223.

BTW: Wouldn’t it be better to use pyhon3 than python2 ?

Say what. But jython (an implementation of Python IN Java) is only available as v2.

Don’t understand to much of that stuff, but I think P3 is newer (Hirnfurz of me :upside_down_face:). But with your help I found a solution. Pls. don’t laugh:

listOfMembers6 = (str([item.name for item in ir.getItem("gGTags").members]).replace("u'","").replace("[","").replace("]","").replace("'",""))
LOG.info("String of Members 6 :{} ".format(listOfMembers6))

result in Logger:

2020-03-19 17:49:38.223 [INFO ] [jsr223.TEST    ]- String of Members 6 :GTag_1, GTag_2, V10, V10_1, test 

I know that that the variable is now Type “String”.

Original Form:
code:

listOfMembers6 = [item.name for item in ir.getItem("gGTags").members]
LOG.info("List of Members 6 :{} ".format(listOfMembers6))

result in Logger:

2020-03-19 17:49:38.215 [INFO ] [jsr223.TEST      ] - List of Members 6 :[u'GTag_1', u'GTag_2', u'V10', u'V10_1', u'test'] 

Type of variable is “List”

As said, only for nice printout, not for further use of that variable.

I’m just try to learn about Python/Jython/JSR223/NGRE. So I tinkering a bit with the syntax to get more experience. I’d never learned programming, so my knowings are all self-taught and OH3 is coming earlier or later, and I want to go on with OH in the future.

Item names are unicode strings, so…

LOG.warn(type(ir.getItem("Virtual_Switch_1").name))

… returns…

2020-03-19 14:06:57.913 [WARN ] [jsr223.jython.TEST] - <type 'unicode'>

If you put Item names in a list, you have a list of unicode strings…

list_of_members = [item.name for item in ir.getItem("gGTags").members]

str.format() will convert the list elements to their string representation, which is what you are seeing as u’My_Item_Name’. You are getting what you asked for :slightly_smiling_face:! To avoid the unicode strings and their funky string representation, then just create a list of strings…

list_of_members = [str(item.name) for item in ir.getItem("gGTags").members]
2 Likes