gpt4 book ai didi

python - AppVeyor 中的 UTF-8,Python 3.6

转载 作者:太空宇宙 更新时间:2023-11-03 11:21:15 25 4
gpt4 key购买 nike

1。总结

我没有找到如何将非 ascii 符号用于 AppVeyor 构建。


2。设置

我的简单SashaAppVeyorEncoding.py文件:

print('Саша наилучшая!')

我的简单appveyor.yml文件:

environment:

matrix:

- PYTHON: "C:\\Python36-x64"
PYTHON_VERSION: "3.6.3"
PYTHON_ARCH: "64"
PIP: "C:\\Python36-x64\\Scripts\\pip"

platform: x64

build_script:

- cmd: "%PYTHON%\\python SashaAppVeyorEncoding.py"

我以 UTF-8 编码保存的两个文件。


3。预期行为

如果我在终端或解释器中运行 SashaAppVeyorEncoding.py 文件 SublimeREPL ,我得到:

D:\SashaPythonista>python SashaAppVeyorEncoding.py
Саша наилучшая!

如果我的 SashaAppVeyorEncoding.py 文件不包含西里尔符号:

print('Sasha superior!')

AppVeyor 构建 successful passed :

Build started
git clone -q --branch=master https://github.com/Kristinita/SashaPythonista.git C:\projects\sashapythonista-7l3yk
git checkout -qf 3a0393a5b9548a5debabebfc5e28d17f3000a768
%PYTHON%\python SashaAppVeyorEncoding.py
Sasha superior!
Discovering tests...OK
Build success

4。实际行为

My AppVeyor build failed :

Build started
git clone -q --branch=master https://github.com/Kristinita/SashaPythonista.git C:\projects\sashapythonista-7l3yk
git checkout -qf 262cef287d45b1548640b9a773b680de90b7d138
%PYTHON%\python SashaAppVeyorEncoding.py
Traceback (most recent call last):
File "SashaAppVeyorEncoding.py", line 1, in <module>
print('\u0421\u0430\u0448\u0430 \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0430\u044f!')
File "C:\Python36-x64\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-3: character maps to <undefined>
Command exited with code 1

5。没有帮助

  1. 我在 SashaAppVeyorEncoding.py 文件的顶部添加了 # -*- coding: utf-8 -*-
  2. add chcp 65001命令到 appveyor.yml 文件,
  3. install win-unicode-consoleappveyor.yml 文件,

我更新的 SashaAppVeyorEncoding.py 文件:

# -*- coding: utf-8 -*-
print('Саша наилучшая!')

我更新的 appveyor.yml 文件:

environment:

matrix:

- PYTHON: "C:\\Python36-x64"
PYTHON_VERSION: "3.6.3"
PYTHON_ARCH: "64"
PIP: "C:\\Python36-x64\\Scripts\\pip"

platform: x64

install:

- cmd: "%PIP% install win-unicode-console"
- cmd: chcp 65001

build_script:

- cmd: "%PYTHON%\\python SashaAppVeyorEncoding.py"

我的 updated AppVeyor build :

Build started
git clone -q --branch=master https://github.com/Kristinita/SashaPythonista.git C:\projects\sashapythonista-7l3yk
git checkout -qf 11df07d4c424cd8e28a1b0db0f43906aa63f42f1
Running Install scripts
%PIP% install win-unicode-console
Collecting win-unicode-console
Downloading win_unicode_console-0.5.zip
Installing collected packages: win-unicode-console
Running setup.py install for win-unicode-console: started
Running setup.py install for win-unicode-console: finished with status 'done'
Successfully installed win-unicode-console-0.5
chcp 65001
Active code page: 65001
%PYTHON%\python SashaAppVeyorEncoding.py
Traceback (most recent call last):
File "SashaAppVeyorEncoding.py", line 2, in <module>
print('\u0421\u0430\u0448\u0430 \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0430\u044f!')
File "C:\Python36-x64\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-3: character maps to <undefined>
Command exited with code 1

6。本地环境

操作系统和版本:
Windows 10 企业版 LTSB 64 位 EN
python :
3.6.3
chcp:
事件代码页:65001

最佳答案

看起来 python 没有打印到 AppVeyor 上的控制台——输出被重定向了。因此locale.getpreferredencoding()用于在打印到标准输出时将 Unicode 文本编码为字节。 cp1252 仅支持百万个 Unicode 字符中的几个。要在此处更改 sys.stdout.encoding,您可以 set PYTHONIOENCODING=utf-8 envvar -- utf-8字符编码支持所有Unicode字符。

关于python - AppVeyor 中的 UTF-8,Python 3.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42644184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com