我需要什么编码才能在 Windows XP 中的 cygwin 上使用 python 显示英镑符号(英镑符号)?

发布于 2024-07-16 08:34:32 字数 485 浏览 7 评论 0原文

我有一个 python (2.5.4) 脚本,在 cygwin 中运行(在 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

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

# coding=cp437

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

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

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

或者我可以去哪里寻找答案?

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:

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:

# 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.

Does anyone know which encoding I need?

Or where I could look to find out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

永言不败 2024-07-23 08:34:32

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

print u"\xA3"

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.

print u"\xA3"
不…忘初心 2024-07-23 08:34:32

尝试编码:

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

然后显示 '£' 符号:

print unichr(163)

try the encoding :

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

and then to display the '£' sign:

print unichr(163)
温折酒 2024-07-23 08:34:32

这里涉及两种编码:

  • 源代码的编码,必须正确,以便输入文件能够表达您所认为的含义
  • 输出的编码,必须正确,以便发出的符号显示为预期的。

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

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

>>> ord("£")
156

156 肯定不是您尝试的 Latin1 编码中井号的代码点。 它似乎不在 Window 的代码页中1252 要么,我希望我的终端使用...奇怪。

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.

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

>>> ord("£")
156

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文