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


Pass unicode instead of string from robot framework to python

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

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"myfile.gzip")

  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 -


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

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