2

I'm running into an issue dealing with RAW datatype column when pulling data from SAP using PyRFC.

Below is the screenshot of the metadata and how the data looks in SAP GUI.

enter image description here

enter image description here

Below is the snippet of the code I'm running to pull this column.

with Connection(user=user, passwd=password, ashost=host, sysnr=sysnr, client=client) as connObj:
    dataObj = connObj.call(table)
    print dataObj

    for x in dataObj.get('PT_LIST'):
        print "type: ", type(x["PARENT"])
        print "parent: ", x["PARENT"]

I'm getting the following response for print dataObj.

{u'PT_LIST': [{u'PARENT': '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'}, {u'PARENT': '\xa06\x9f\xa8\\\xb4\x1e\xda\xac\x97\x18-\xf5\xb4\x1f\xe8'}]}

type:  <type 'str'>
parent:
type:  <type 'str'>
parent:  ▒6▒▒\▒ڬ▒-▒▒

and when I try to convert it to str, I'm getting the following error.

Traceback (most recent call last):
  File "<stdin>", line 18, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 0: ordinal not in range(128)

Could someone please help me understand how to properly convert this data to a readable format the same as how it shows in SAP GUI.

Appreciate your time and response.

Thanks,

P Raju
  • 329
  • 2
  • 14
  • 1
    "byte 0xa0 in position 0" means that you successfully retrieve the first byte from SAP return. "'ascii' ... not in range(128)" means that your current code converts the bytes into characters using ASCII US (ANSI) which doesn't accept bytes with value 0x80 (128) and above. But I guess you just want to convert bytes into hexadecimal string. Search the web for a solution. – Sandra Rossi Jul 21 '20 at 06:37
  • 1
    For python 2.6, here is a [possible solution](https://stackoverflow.com/a/50890115/9150270). – Sandra Rossi Jul 21 '20 at 07:26
  • 1
    @SandraRossi Thank you so much for the insights. Your directions help me find the solution. I've been trying utf-8 encode but turns out I need to use hex encode. This worked for me [link](https://stackoverflow.com/a/16033232/6923547) – P Raju Jul 21 '20 at 08:07

1 Answers1

2

I've modified my code as below which worked like magic. Special thanks to @Sandra Rossi for the suggestions.

with Connection(user=user, passwd=password, ashost=host, sysnr=sysnr, client=client) as connObj:
    dataObj = connObj.call(table)
    for x in dataObj.get('PT_LIST'):
        print "parent: ", x["PARENT"].encode('hex')

Output:

parent:  00000000000000000000000000000000
parent:  a0369fa85cb41edaac97182df5b41fe8

encode('hex') did the trick. I've been trying utf-8 and other encodings while I should be using hex in this case.

P Raju
  • 329
  • 2
  • 14