是什么导致在我的第一个Python脚本中打印额外的数字?(代码页/unicode版本)

2024-10-02 16:32:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我是第一次学习python3的C程序员。这是我的第一个剧本。你知道吗

# -*- coding: utf-8 -*-
print ("Hello World!")
print ("Ä 0196, Ë 0203, Ï 0207, Ö 0214, Ü 0220, Ÿ 0159.")

以下是一些设置和输出:

C:\Users\K\Desktop\Code\Python\Korgan\LearnPythonTheHardWay>chcp 65001
Active code page: 65001

C:\Users\K\Desktop\Code\Python\Korgan\LearnPythonTheHardWay>set PYTHONIOENCODING=utf-8

C:\Users\K\Desktop\Code\Python\Korgan\LearnPythonTheHardWay>py ex1.py
Hello World!
Ä 0196, Ë 0203, Ï 0207, Ö 0214, Ü 0220, Ÿ 0159.
159.

C:\Users\K\Desktop\Code\Python\Korgan\LearnPythonTheHardWay>

它多印了一个“159”字,在结尾加了一个新行。为什么?你知道吗

“py”不带参数:

PS C:\Users\K\desktop\code\Python\Korgan\LearnPythonTheHardWay> py
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Tags: pyhelloworldcodeuserspython3utf程序员
1条回答
网友
1楼 · 发布于 2024-10-02 16:32:23

Windows控制台不支持代码页65001,并且在尝试时会做一些奇怪的事情,这是由于潜在的字符计数错误造成的。这会影响所有使用C标准IO接口的应用程序,包括Python。你知道吗

如果您真的,真的需要将Unicode连接到控制台,那么可以通过直接调用Win32特定的控制台api来解决这个问题。见win_unicode_console。你知道吗

相关问题 更多 >