【问题标题】:Issue ingesting RAW datatype column using PyRFC使用 PyRFC 提取 RAW 数据类型列的问题
【发布时间】: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


【解决方案1】:

我已经修改了我的代码,如下所示,它就像魔术一样工作。特别感谢@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 和其他编码,而在这种情况下我应该使用十六进制。

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 2014-07-08
    • 2015-10-10
    相关资源
    最近更新 更多