简体   繁体   English

Azure Function Python 字符串编码问题

[英]Azure Function Python String Encoding Issue

I am having an issue with an Azure Function in Python.我在 Python 中遇到 Azure Function 的问题。

The following code:以下代码:

test = b'\xce\x94'
print(test)
a = test.decode('utf-8')
print(a)

prints the following if I run it in Python 3.9 on my PC running Windows 10.如果我在运行 Windows 10 的 PC 上以 Python 3.9 运行它,则会打印以下内容。

b'\xce\x94'

Δ

This is correct as it should result in the Delta character.这是正确的,因为它应该导致 Delta 字符。

If I run the same code in an Azure Function App (also Python 3.9) it errors if I try to print it如果我在 Azure Function 应用程序(也是 Python 3.9)中运行相同的代码,如果我尝试打印它会出错

System.Private.CoreLib: Exception while executing function: Functions.HttpTrigger. System.Private.CoreLib: Result: Failure
Exception: UnicodeEncodeError: 'charmap' codec can't encode character '\u0394' in position 0: character maps to <undefined>
Stack:   File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 407, in _handle__invocation_request
    call_result = await self._loop.run_in_executor(
  File "C:\Program Files\Python39\lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 649, in _run_sync_func
    return ExtensionManager.get_sync_invocation_wrapper(context,
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\extension.py", line 215, in _raw_invocation_wrapper
    result = function(**args)
  File "C:\repo\Data\XXXX\XXXXXXXX\HttpTrigger\__init__.py", line 75, in main
    print(a)
  File "c:\Users\XXXXX\.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_io.py", line 40, in write
    r.write(s)
  File "C:\Program Files\Python39\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]

or shows a或显示一个

?

if I log it using Python logging library.如果我使用 Python 日志库记录它。

Can anyone help please?有人可以帮忙吗?

  • I think the issue is with the e in the test it is probably thinking that it is a latin1 character and that might be why it is giving the error.我认为问题出在test中的e它可能认为它是一个 latin1 字符,这可能就是它给出错误的原因。

  • The remedy for this would be to encode this in latin1 first and then decode this to utf-8对此的补救措施是先将其编码为latin1 ,然后将其解码为utf-8

test.encode('latin1').decode('utf-8');
  • here I deployed the azure function return the decoded test string这里我部署了 azure function 返回解码后的测试字符串

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM