lambda 环境中的解码方法 returns unicode 响应

decode method returns unicode response in lambda env

在 Python 3.7 环境中解码函数在 aws lambda returns 1\u0000\u0000\u0000\u000001 at [1] while in local python 3.7.2 interpreter it returns 101

def lambda_handler(event, context):

    data = b'1\x00\x00\x00\x0001'
    response = data.decode()
    print(response)#[1]
    return {
        'statusCode': 200,
        'body': str(response)
    }

当本地翻译时,

>>> data = b'1\x00\x00\x00\x0001'
>>> print (data.decode())
101

我还需要 101 作为 lambda 3.7 解释器的响应。 欢迎提出任何建议。

只需删除字符串中的空值 (\x00)。如果您的字符串中有这些空值,打印将无法正确输出。

data.decode('utf8').replace('\x00', '')