-2

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.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
Illusionist
  • 5,204
  • 11
  • 46
  • 76

1 Answers1

1

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'
Community
  • 1
  • 1
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Well I am trying to compare it after, but in the comparison it seems that I am trying to compare a unicode with a non unicode string so its not working. I tried printing with repr() and the result seems to be the same. Thanks for the default argument link though. – Illusionist Dec 05 '16 at 11:00
  • You should show the code you are actually having problems with, then. It's perfectly possible to compare unicode with bytestrings in Python 2. – Daniel Roseman Dec 05 '16 at 11:02
  • I think you need to show what data you are passing in, and how. – Daniel Roseman Dec 05 '16 at 11:11