Unicode解码错误:'charmap'编解码器无法解码位置 8024 中的字节0x9d:<undefined>将输出重定向到磁盘时字符映射到



在Windows 10上,我使用httpie来检索数据。当在屏幕上显示时,它工作得很好,但一旦我将输出管道传输到磁盘,我就会得到http:error:UnicodeDecodeError:"charmap"编解码器无法解码位置8024中的字节0x9d:字符映射到<undefined>

写入磁盘时有没有办法更改编码?

以下是完整的调试输出:


>> http --debug --print=Bb --pretty=all --session=C:UsersPETERK~1AppDataLocalTemp/access.json http://localhost:8080/nexus/extract/path  filePath=c:/TestFiles/100Mb.pdf > foo
HTTPie 2.3.0
Requests 2.25.1
Pygments 2.7.3
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec  7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)]
c:pythonpython39python.exe
Windows 10
<Environment {'colors': 256,
'config': {'default_options': []},
'config_dir': WindowsPath('C:/Users/PeterKronenberg/AppData/Roaming/httpie'),
'devnull': <property object at 0x000001112C1815E0>,
'is_windows': True,
'log_error': <function Environment.log_error at 0x000001112C1AC1F0>,
'program_name': 'http',
'stderr': <colorama.ansitowin32.StreamWrapper object at 0x000001112C176F10>,
'stderr_isatty': True,
'stdin': <_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>,
'stdin_encoding': 'utf-8',
'stdin_isatty': True,
'stdout': <colorama.ansitowin32.StreamWrapper object at 0x000001112C176BE0>,
'stdout_encoding': 'cp1252',
'stdout_isatty': False}>
>>> requests.request(**{'auth': None,
'data': '{"filePath": "c:/TestFiles/100Mb.pdf"}',
'headers': {'User-Agent': b'HTTPie/2.3.0', 'Accept': b'application/json, */*;q=0.5', 'Content-Type': b'application/json', 'authorization': b'eyJhbGciOiJSUzI1NiIsImtpZCI6InB1YmxpYzplMjM3ODE1Ny04MmY0LTQ2YjItYWI5NS1hMDRhMzg4ZGY5ZTQiLCJ0eXAiOiJKV1QifQ.eyJhdWQiOltdLCJjbGllbnRfaWQiOiIxMTExMTExMS0xMTExLTExMTEtMTExMS0xMTExMTExMTExMTUiLCJleHAiOjE2MDk0NDAxNzYsImV4dCI6e30sImlhdCI6MTYwOTQzNjU3NiwiaXNzIjoiaHR0cHM6Ly9vaWRjLnRvcmNoLmFpLyIsImp0aSI6IjZkN2QwOTFkLTQyMTctNGRmMS04ZmY3LTk2MDdiMzkzZjBlZCIsIm5iZiI6MTYwOTQzNjU3Niwic2NwIjpbXSwic3ViIjoiMTExMTExMTEtMTExMS0xMTExLTExMTEtMTExMTExMTExMTE1In0.QeCNhpoE-1UyLcHer2_JjhiMx3qKV1qIC3LX0rlRUKSIK5vlMbJ2KmVP1mNR647SJhAxN9zdqf9vDaG6Fk0ZRLBFSBbcq0WuyTlaJsPVVggpe9ys_mcqO8uPMTl4-tO0H5kXVzRi_TFcmfCH8W3X8KAe3K7kjfs8O207EAenPr8n-KB12qUQgpC4ELXRbC_9SwuZY_ZhOic9POUYMCvrmJkMB8Th4kOqix5uI45lwowVQI9kTgAB4kQF8VelYJF7xxxn81U24T3ab9lRtp6ShY1mUkhDwfhrz2YdhYQ4uhQhKNZc3Hxz8OPSV_0Q-ykdZVgi03su51E_SE0odnryiUrR4nheu1cxmGmKNq8gb2CII9NktgrZEvvXlYJ5cV5mI2AAVlgkp4A0FBCd_wt39mXadIWPjv3YfilkzwUZCoeKA8dpp1mOKebFjq7Iw4LnxhObuKw1BnhYjHzdtQC5mhwFtc5iCeYChkQG_b8vybap12S4HzQ7HHFBKjdJZ2Il5mbuF01BAUBhZ1TO2GQ_t3O0JVrp8rmob75Yiza5FiBZaeAASSp2p0mb-_CmTVnQq3N-f099yL7byNYKl0toLIfgDLuib6bv_rhushdvEyWOD1c-nJnqMNFekEZ4NXTvBskL9PJCWcW-r8l1Fn567s7SAFan-G37YeQYBfmwADI'},
'method': 'post',
'params': <generator object MultiValueOrderedDict.items at 0x000001112CA5FB30>,
'url': 'http://localhost:8080/nexus/extract/path'})

http: error: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1617: character maps to <undefined>
Traceback (most recent call last):
File "c:pythonpython39librunpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:pythonpython39librunpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:PythonPython39Scriptshttp.exe__main__.py", line 7, in <module>
File "c:pythonpython39libsite-packageshttpie__main__.py", line 11, in main
exit_status = main()
File "c:pythonpython39libsite-packageshttpiecore.py", line 79, in main
exit_status = program(
File "c:pythonpython39libsite-packageshttpiecore.py", line 222, in program
write_message(
File "c:pythonpython39libsite-packageshttpieoutputwriter.py", line 38, in write_message
write_stream_with_colors_win_py3(**write_stream_kwargs)
File "c:pythonpython39libsite-packageshttpieoutputwriter.py", line 82, in write_stream_with_colors_win_py3
outfile.write(chunk.decode(encoding))
File "c:pythonpython39libencodingscp1252.py", line 15, in decode
return codecs.charmap_decode(input,errors,decoding_table)
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1617: character maps to <undefined>

HTTPie从系统的stdout配置中获取编码。您已经为Windows 1252配置了它,并试图用它的功能(可能是utf8(写下数据。

虽然HTTPie没有提供单独配置编写器输出编码的选项,但您可以通过PYTHONIOENCODING环境变量设置输出的默认值。试试这个

PYTHONIOENCODING=utf-8 http --debug --print=Bb --pretty=all --session=C:UsersPETERK~1AppDataLocalTemp/access.json http://localhost:8080/nexus/extract/path  filePath=c:/TestFiles/100Mb.pdf > foo

事实上,我对Windows并不太熟悉——如果这不起作用,请在运行HTTPie:之前设置运行这个

setx PYTHONIOENCODING "utf-8"

请注意,这将覆盖此命令行会话的所有其他Python脚本的编码。

相关内容

  • 没有找到相关文章

最新更新