How do I turn an array into a list without the characters (u') showing up in the result?

This is what I have in my script transform. It works for "KIT" (I think so because it's only one value), but not for "barcode" which has multiple numbers. I get this for BARCODE: 0000003520240', u'0000003520449', u'0000003895613', u'0000004443326', u'0000004501648

i = 0
kit = []
bar = []
lot = []
par = []
table = value
kit.append(table.getValueAt(0,0))
if table.getRowCount() >= 0:
	while i < table.getRowCount():
		bar.append(table.getValueAt(i, 'BARCODE'))
		lot.append(table.getValueAt(i, 'LOT'))
		par.append(table.getValueAt(i, 'PART'))
		i = i + 1

res_kit = str(kit)[1:-1]
res_kit = str(res_kit)[1:-1]
res_kit = str(res_kit)[1:]

for x in bar:
	res_bar = str(bar)[1:-1]
	res_bar = str(res_bar)[1:-1]
	res_bar = str(res_bar)[1:]
  
   return  [{'KIT':res_kit, 'BARCODE':res_bar}]

Why are you wrapping things in str()? When the wrapped item isn't string-like, you are at the mercy of jython's guesses as to how to make it a string. For complex objects, that usually means calling jython's repr() built-in function. Since jython strings, unlike real python 2.7, are really unicode, you get the u"" format when they are nested in complex objects. In most cases.

Don't blindly wrap stuff in str(). Or in unicode(). If in doubt what to do, examine the type() of the item to determine the best way to handle it.

1 Like

This is alien language to me. What the hell is this supposed to do ?
First, make sure this is actually what you're running, because this shouldn't even run:
You have mixed indentation, especially on the return. Is it in the loop, or at the top level ?

Then, please explain what's your input and what's your expected output, and I promise we can find a better way to do it.

1 Like

As mentioned, if you avoid doing a str() on Python lists, you won't end up with the u'' characters in most circumstances.

I don't have your dataset to test on, but this should hopefully point you in the right direction. The ", ".join(bar) turns the list into a string and adds commas between each element. I'm guessing that's what you wanted.

If you instead want to return an array for BARCODE instead of a comma separated list:

I also cleaned up a few other items. It didn't seem like lot and par were being used, but of course they could be added back if they'll be used. Doing a for loop with a range() avoids needing to declare or increment an index variable, and it won't execute at all if there are 0 elements so no need for a conditional check for that. Making kit the value of the table instead of declaring it as an array makes it avoid any need for stripping characters, as long as the original format is good. If it's actually a number and needs to be converted to a string, you could do kit = str(table.getValueAt(0,0)) instead of the line above. That shouldn't introduce any new characters, as long as the table's value at 0,0 is actually a number.

Hope this all helps!

Instead of this, you can also use getColumnAsList to produce this.

bar = table.getColumnAsList(table.getColumnIndex('BARCODE'))

https://docs.inductiveautomation.com/display/DOC81/Datasets

2 Likes