【问题标题】:What is causing the extra number to be printed in my first Python script? (codepage/unicode issue)是什么导致在我的第一个 Python 脚本中打印了额外的数字? (代码页/unicode问题)
【发布时间】:2015-12-09 04:38:52
【问题描述】:

我是第一次学习 Python 3 的 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.
>>>

【问题讨论】:

  • 这不是我运行你的程序时得到的结果
  • 请打印以下输出:env| grep -i 编码
  • 什么是“py”程序?如果你在没有参数的情况下运行“py”会发生什么?
  • @fiacre 不幸的是,这是在win8上。
  • @fiacre 我已经更新了我的问题以显示没有参数的“py”。

标签: python unicode utf-8 codepages


【解决方案1】:

由于潜在的字符计数错误,Windows 控制台不支持代码页 65001,并且在您尝试时会执行许多奇怪的事情。这会影响所有使用 C 标准 IO 接口的应用程序,包括 Python。

如果您确实需要将 Unicode 导入控制台,则可以通过直接调用 Win32 特定的控制台 API 来解决此问题。见win_unicode_console

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-14
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2020-08-13
    • 1970-01-01
    • 2016-03-27
    相关资源
    最近更新 更多