Azure Function Python String Encoding Issue

Viewed 32

I am having an issue with an Azure Function in Python.

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.

b'\xce\x94'

Δ

This is correct as it should result in the Delta character.

If I run the same code in an Azure Function App (also Python 3.9) it errors if I try to print it

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.

Can anyone help please?

1 Answers
  • 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.

  • The remedy for this would be to encode this in latin1 first and then decode this to utf-8

test.encode('latin1').decode('utf-8');
  • here I deployed the azure function return the decoded test string

enter image description here

Related