Question: Pass unicode instead of string from robot framework to python

Question

Pass unicode instead of string from robot framework to python

Answers 1
Added at 2016-12-05 11:12
Tags
Question

I have a robot framework file where I am calling a python function and passing it an argument. I want to pass the argument to python as a Unicode, but it keeps getting passed as the string.

Function is

def assert_my_test( var1, var3=[]):
  print "var1" #this i want in unicode, unicode(var1) doesnt help
  f=gzip.open("myfile.gzip")
  lines=f.readlines()

  if any(unicode(var1) in unicode(s) for s in lines):
       print "found our line"

Robot is

========================  =====================  ======================= 
Test Case                 Action                 Argument
========================  =====================  ======================= 
test1                     myprog.assert_my_test  \x01\x02\x03

Output is x01x02x03 instead of u"x01x02x03"

Any ideas how to pass Unicode properly? Really appreciated.

I have tried passing "Unicode(var1) to the function definition but that results in an error.

Complete variable i am trying to pass is -

\x01\x02\x03html&h=250&slotname5&adk=1204670.0&url=htt||1|0Ft2qy|4CX000||||17.022qy^A0C5vZBmjiK^ABusiness^DPersonalFin-0^A^A^Afalse^Afalse^A^A^A^A^A^A^A^A^A0^A0^A0^A0^A^A^A1^A^A^A^A1^A^A^A^A6284035007053575^A^A^Aen^A^Ab1cc^A^A8281671^Aalias_type^DDX^Calias_id^D4CXDxrs315^C"

I want to find if this variable exists in the file.

Answers
nr: #1 dodano: 2016-12-05 11:12

When you print a value, naturally you get the printable representation of that value.

I have no idea why you would want this, but you can get your desired output by calling repr():

print repr(var1)

(Also, not related to your problem, but don't use a list as a default argument: see this question as to why.)

Edit So I'm still not entirely sure what you are trying to do, but I think what is going on is that you have a raw string consisting of slashes, Xs and digits, not bytes. You can convert that to unicode by decoding it using the "unicode-escape" codec:

var1 = r'\x01\x02\x03'
decoded = var1.decode('unicode-escape')  # result: u'\x01\x02\x03'
Source Show
◀ Wstecz