在 Windows XP 中的 cygwin 上使用 python 显示 GBP 符号(井号)需要什么编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/705434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:42:19  来源:igfitidea点击:

What encoding do I need to display a GBP sign (pound sign) using python on cygwin in Windows XP?

pythonencodingpython-2.5

提问by Ben

I have a python (2.5.4) script which I run in cygwin (in a DOS box on Windows XP). I want to include a pound sign (£) in the output. If I do so, I get this error:

我有一个在 cygwin 中运行的 python (2.5.4) 脚本(在 Windows XP 的 DOS 框中)。我想在输出中包含一个英镑符号 (£)。如果我这样做,我会收到此错误:

SyntaxError: Non-ASCII character '\xa3' in file dbscan.py on line 253, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

OK. So I looked at that PEP, and now tried adding this to the beginning of my script:

行。所以我查看了那个 PEP,现在尝试将它添加到我的脚本的开头:

# coding=cp437

That stopped the error, but the output shows ú where it should show £.

这停止了​​错误,但输出显示 ú 应该显示 £ 的地方。

I've tried ISO-8859-1 as well, with the same result.

我也尝试过 ISO-8859-1,结果相同。

Does anyone know which encoding I need?

有谁知道我需要哪种编码?

Or where I could look to find out?

或者我可以在哪里寻找?

采纳答案by unwind

There are two encodings involved here:

这里涉及两种编码:

  • The encoding of your source code, which must be correct in order for your input file to mean what you think it means
  • The encoding of the output, which must be correct in order for the symbols emitted to display as expected.
  • 您的源代码的编码,必须正确才能让您的输入文件表示您认为的意思
  • 输出的编码,必须正确才能发出的符号按预期显示。

It seems your output encoding is off now. If this is running in a terminal window in Cygwin, it is that terminal's encoding that you need to match.

看来您的输出编码现在已关闭。如果它在 Cygwin 的终端窗口中运行,则您需要匹配该终端的编码。

EDIT: I just ran the following Python program in a (native) Windows XP terminal window, thought it was slightly interesting:

编辑:我刚刚在(本机)Windows XP 终端窗口中运行了以下 Python 程序,认​​为它有点有趣:

>>> ord("£")
156

156 is certainly not the codepoint for the pound sign in the Latin1 encoding you tried. It doesn't seem to bein Window's Codepage 1252 either, which I would expect my terminal to use ... Weird.

156 肯定不是您尝试的 Latin1 编码中英镑符号的代码点。它不似乎是在窗口的代码页1252要么,我会想到我的终端使用...怪异。

回答by rjmunro

The Unicode for a pound sign is 163 (decimal) or A3 in hex, so the following should work regardless of the encoding of your script, as long as the output encoding is working correctly.

英镑符号的 Unicode 是 163(十进制)或 A3 十六进制,因此,只要输出编码正常工作,无论您的脚本编码如何,以下内容都应该有效。

print u"\xA3"

回答by djibril

try the encoding :

尝试编码:

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

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

and then to display the '£' sign:

然后显示“£”符号:

print unichr(163)