Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么要打印字符串;H&xFC;hnchen“;在Windows 7下的命令提示符中,导致一个;没有这样的文件或目录;错误?_Windows_Python 2.7_Cmd_Character Encoding - Fatal编程技术网

为什么要打印字符串;H&xFC;hnchen“;在Windows 7下的命令提示符中,导致一个;没有这样的文件或目录;错误?

为什么要打印字符串;H&xFC;hnchen“;在Windows 7下的命令提示符中,导致一个;没有这样的文件或目录;错误?,windows,python-2.7,cmd,character-encoding,Windows,Python 2.7,Cmd,Character Encoding,执行以下python程序 test.py: # -*- coding: utf-8 -*- print "Hühnchen" hexdump: 00000000 23 20 2d 2a 2d 20 63 6f 64 69 6e 67 3a 20 75 74 |# -*- coding: ut| 00000010 66 2d 38 20 2d 2a 2d 20 0a 0a 70 72 69 6e 74 20 |f-8 -*- ..print | 00000020 22 48 c

执行以下python程序

test.py:

# -*- coding: utf-8 -*- 

print "Hühnchen"
hexdump:

00000000  23 20 2d 2a 2d 20 63 6f  64 69 6e 67 3a 20 75 74  |# -*- coding: ut|
00000010  66 2d 38 20 2d 2a 2d 20  0a 0a 70 72 69 6e 74 20  |f-8 -*- ..print |
00000020  22 48 c3 bc 68 6e 63 68  65 6e 22 0a              |"H..hnchen".|
在代码为65001的Windows 7和Lucida Console字体的命令提示符下,会导致
IOError

$ python test.py
HühnchenTraceback (most recent call last):
  File "test.py", line 3, in <module>
    print "Hühnchen"
IOError: [Errno 2] No such file or directory
$python test.py
Hühnchen回溯(最近一次通话最后一次):
文件“test.py”,第3行,在
打印“Hühnchen”
IOError:[Errno 2]没有这样的文件或目录
为了排除Windows安装的任何副作用,我通过以下步骤在新的虚拟机中重现了该问题:

  • 在虚拟机中安装Windows 7 Ultimate SP1 Build 7601(ISO:36ae90defbad9d9539e649b193ae573b77a71c83的SHA1
  • 安装Python2.7.13 64位
  • 打开
    cmd.exe
  • 将字体设置为Lucida Console
  • 将代码页更改为65001以支持UTF-8
  • 执行上述脚本(确保文件编码为UTF-8)
  • 同样的结果是:


    这里发生了什么?

    因此,我可以重新创建此错误的唯一方法是更改为Lucida并在运行程序之前运行chcp 65001命令。根据这篇文章,“在Windows8之前,使用代码页65001的输出也被破坏”[1]。如果您只想打印“Hühnchen”或任何来自UTF-8的字符,我认为最好的解决方案是不要从命令行这样做,因为它似乎总是会弄乱字符。当我在Pyce上运行相同的代码时,它工作得很好,甚至当我打开Python2.7命令提示符时,它仍然正确地打印出来。这似乎是一个非常利基的问题,你正在经历的,应该能够解决

    [1]