如何在Python中打印欧元(€)符号? [英] How can I print a euro (€) symbol in Python?
问题描述
我正在使用命令行解释器(适用于Windows的v3.5)自学Python.
I'm teaching myself Python using the command-line interpreter (v3.5 for Windows).
我要做的就是输出一些包含欧元(€)符号的文本,据我了解,该代码为80h(12月128日).
All I want to do is output some text that includes the euro (€) symbol which I understand to be code 80h (128 dec).
#!
# -*- coding: utf-8 -*-
mytext = 'Please pay \x8035.'
print(mytext)
它落在最后一行:
UnicodeEncodeError: 'charmap' codec can't encode character '\x80' in position 11: character maps to <undefined>
我做了很多谷歌搜索(重新编码等),并且我大概知道为什么打印命令失败.修改上面的代码表明,正如人们所期望的那样,最高\ x7f的ASCII代码可以正常工作.
I've done lots of googling (re encodings etc) and I've a rough idea why the print command fails. Tinkering with the above code shows that ASCII codes up to \x7f work fine, as one might expect.
但是我不知道如何显示€,而且我发现有关编码的信息不堪重负且难以理解. (记住我只是菜鸟!)
But I can't figure out how to display the €, and I'm finding the information on encodings overwhelming and impenetrable. (Remember I'm just a noob!)
- 我尝试使用 u 为文本字符串添加前缀,以首先创建一个unicode字符串.
- 我尝试创建一个中间对象 outputtext = mytext.encode('utf-8'),但使用 print 输出此对象会将字符串扩展为更加神秘的字符串格式: b'请支付\ xc2 \ x8035.'
- 我试图找到一个替代函数来代替 print 来输出中间字符串,但是还没有任何效果.
- I've tried prefixing the text string with u to create a unicode string in the first place.
- I've tried creating an intermediate object outputtext = mytext.encode('utf-8') but outputting this with print expands the string into an even more cryptic form: b'Please pay \xc2\x8035.'
- I've tried to find a different function instead of print to output this intermediate string, but nothing has worked yet.
请有人可以告诉我一些可以正常工作的代码,以便我可以对其进行研究并从那里反向进行工作.谢谢!
Please can someone show me some code that just works, so I can study it and work backwards from there. Thanks!
推荐答案
以下是在Python 3.x中以模糊度递增的顺序打印带有欧元符号的字符串的四种方法.
Here are four ways of printing a string with the Euro symbol in Python 3.x, in increasing order of obscurity.
1.直接输入
使用键盘输入符号,或从其他位置复制并粘贴符号:
Use your keyboard to enter the symbol, or copy and paste it from somewhere else:
mytext = "Please pay €35."
print(mytext)
2.使用Unicode字形数字
例如从非常有用的页面 http://www.fileformat .info/info/unicode/,然后在您的字符串中使用该代码:
Look up the Unicode glyph number, for example from the very useful page http://www.fileformat.info/info/unicode/, and use that code in your string:
mytext = "Please pay \u20ac35."
print(mytext)
3.使用字形名称
您可以使用unicodedata
模块中的 lookup()
进行访问Unicode字形的名称.再次 http://www.fileformat.info/info/unicode/可以帮助您找到字形名称:
You can use lookup()
from the unicodedata
module to access Unicode glyphs by name. Again http://www.fileformat.info/info/unicode/ will help you find the glyph name:
import unicodedata
mytext = "Please pay {}35.".format(unicodedata.lookup("EURO SIGN"))
print(mytext)
4.使用Windows-1252代码页
如果您确实要使用\x80
字节码,则可以这样做,因为它在Windows-1252代码页中代表欧元符号.您需要做的是首先创建一个包含该字节代码的字节字符串,然后对该字节字符串进行解码,以便将该字节代码转换为Windows-1252代码页中的欧元符号条目:
If you really want to use the \x80
byte code, you can do that, because it represents the Euro symbol in the Windows-1252 codepage. What you need to do is first create a byte string containing that byte code, and then decoding that byte string so that the byte code is translated to the Euro symbol entry in the Windows-1252 codepage:
mytext = b"Please pay \x8035.".decode("windows-1252")
print(mytext)
这篇关于如何在Python中打印欧元(€)符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!