【发布时间】:2020-07-21 05:48:50
【问题描述】:
我在使用 PyRFC 从 SAP 提取数据时遇到了处理 RAW 数据类型列的问题。
以下是元数据的屏幕截图以及数据在 SAP GUI 中的外观。
下面是我为拉出此列而运行的代码的 sn-p。
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 中的显示方式一样。
感谢您的时间和响应。
谢谢,
【问题讨论】:
-
"byte 0xa0 in position 0" 表示您成功从 SAP 返回中检索到第一个字节。 “'ascii' ... not in range(128)”表示您当前的代码使用不接受值为 0x80 (128) 及以上的字节的 ASCII US (ANSI) 将字节转换为字符。但我猜你只是想将字节转换为十六进制字符串。在网上搜索解决方案。
-
对于python 2.6,这里是possible solution。
-
@SandraRossi 非常感谢您的见解。您的指示帮助我找到解决方案。我一直在尝试 utf-8 编码,但结果我需要使用十六进制编码。这对我有用link
标签: python python-2.6 sap-gui pyrfc