在记事本中使用 Python 使瑞典语字符在 Windows 命令提示符中正确显示++
标题很好地解释了这一点。我已经设置了 Notepad++,当我按 F8 时,在命令提示符中打开 Python 脚本,但在 CMD 中打开时,所有瑞典字符看起来都很混乱,但在 IDLE 等中则完全正常。
这个简单的示例代码:
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
print "åäö"
看起来像这个。
正如您所看到的,我用来在下面的 cmd 中打开 Python 的批处理文件的输出正确显示了字符,但上面的 Python 脚本却没有显示。我该如何解决这个问题?我只是想正确显示字符我不一定也使用UTF-8。
我使用此 方法 在 cmd 中打开该文件。
更新:已解决。在批处理文件顶部添加了“chcp 1252”行,然后在其下方添加了 cls 行,以删除有关其使用的字符编码的消息。然后我在python脚本中使用“# --coding: cp1252 --”并将cmd中的字体更改为Lucida Console。这是通过单击 cmd 窗口右上角的 cmd 图标并进入属性来完成的。
The title explains it well. I have set up Notepad++ to open the Python script in the command prompt when I press F8 but all Swedish characters looks messed up when opening in CMD but perfectly fine in e.g IDLE.
This simple example code:
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
print "åäö"
Looks like this.
As you can see the output of the batch file I use to open Python in cmd below shows the characters correctly but not the Python script above it. How do I fix this? I just want to show the characthers correctly I dont necessarily have too use UTF-8.
I open the file in cmd using this method.
Update: Solved. Added a "chcp 1252" line at the top of the batch file and then a cls line below it to remove the message about what character encoding it uses. Then I used "# -- coding: cp1252 --" in the python script and changed the font in cmd to Lucida Console. This is done by clicking the cmd icon at the top right of the cmd window and go into properties.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在打印 UTF-8 字节,但您的控制台未设置为 UTF-8。将 Unicode 写为 UTF-16,或者将控制台代码页设置为 UTF-8。
You're printing out UTF-8 bytes, but your console is not set to UTF-8. Either write Unicode as UTF-16, or set your console codepage to UTF-8.
我遇到了同样的问题,我使用了 cp1252
这使得控制台使用 1252 编码,然后我运行我的程序,该程序显示了瑞典字符。
I had the same issue and I used
cp1252
This made the console to use 1252 encoding and then I ran my program which displayed the swedish characters with mercy.
将编码设置为:
# -*- 编码:ISO-8859-1 -*-
这对我有用,我尝试了很多不同的解决方案以使其能够与 Visual Studio IDE for Python 一起使用。
Set coding to:
# -*- coding: ISO-8859-1 -*-
This worked for me and I tried a lot of different solutions to get it to work with Visual Studio IDE for Python.
Python 通常会将 Unicode 字符串转换为 Windows 控制台的编码。请注意,要正确使用 Unicode,您需要 Unicode 字符串(例如 u'string'),并且需要使用
coding:
行声明保存文件的编码。例如,(在我的系统上以 UTF-8 格式保存为 x.py):
产生这样的结果:
您只能成功打印活动代码页支持的字符。
Python will normally convert Unicode strings to the Windows console's encoding. Note that to use Unicode properly, you need Unicode strings (e.g., u'string') and need to declare the encoding the file is saved in with a
coding:
line.For example, this (saved in UTF-8 as x.py on my system):
Produces this:
You'll only be able to successfully print characters that are supported by the active code page.