使用PyRFC接收原始数据类型列的问题

2024-09-22 20:21:34 发布

您现在位置:Python中文网/ 问答频道 /正文

在使用PyRFC从SAP提取数据时,我遇到了一个处理原始数据类型列的问题

下面是元数据的屏幕截图以及数据在SAP GUI中的外观

enter image description here

enter image description here

下面是我正在运行以获取本专栏的代码片段

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"]

我得到了对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▒▒\▒ڬ▒-▒▒

当我尝试将其转换为str时,我得到以下错误

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)

请有人帮助我理解如何正确地将这些数据转换为可读格式,与SAP GUI中显示的格式相同

感谢您的时间和回复

谢谢


Tags: 数据inclienttypeguiparentsapprint
1条回答
网友
1楼 · 发布于 2024-09-22 20:21:34

我已经修改了我的代码,如下所示,它像魔术一样工作。特别感谢@Sandra Rossi的建议

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')

输出:

parent:  00000000000000000000000000000000
parent:  a0369fa85cb41edaac97182df5b41fe8

encode('hex')成功了。我一直在尝试utf-8和其他编码,而在这种情况下我应该使用十六进制

相关问题 更多 >