本文是小编为大家收集整理的关于如何打印unicode字符串?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我想打印一个 unicode 字符串.
我在 Emacs 中运行 Python,它理解 utf-8,所以我想
强制 Python 将 utf-8 发送到 sys.stdout.
从我用谷歌搜索的内容来看,我认为我需要设置我的语言环境.我没有
了解如何.
导入语言环境
打印 locale.getlocale()
--(无,无)
打印 locale.getdefaultlocal()
--(''en_GB'',''cp1252'')
打印 locale.normalize(''en_GB.utf-8'')
--zh_CN.UTF8
locale.setlocale(locale.LC_ALL,''en_GB.UTF8'')
-- locale.Error: 不支持的区域设置
我会很感激你的建议.
Damon.
推荐答案
da**********@gmail.com 写道:
从我用谷歌搜索的内容来看,我认为我需要设置我的语言环境.我没有只是因为语言环境库知道它的规范化名称
了解如何.
导入语言环境
打印 locale.getlocale()
--(无,无)
打印 locale.getdefaultlocal()
--(''en_GB'',''cp1252'')
打印 locale.normalize(''en_GB.utf-8'')
--zh_CN.UTF8
locale.setlocale(locale.LC_ALL,''en_GB.UTF8'')
-- locale.Error: 不支持的区域设置
我会很感激你的建议.
并不意味着它在您的操作系统上可用.您确认您的
操作系统(独立于 Python)是否支持您尝试设置的语言环境?
--
\ "哦,我喜欢你的杂志.我最喜欢的部分是"如何做 |
`\增加你的文字力量''.那个东西真的,真的,|
_o__) 真的……很好."——荷马,《辛普森一家》_ |
Ben Finney
4 月 19 日凌晨 12:51,Ben Finney 写道:只是因为语言环境库知道它的规范化名称否.我如何知道我的操作系统支持哪些语言环境?(我在跑步
并不意味着它在您的操作系统上可用.您确认您的
操作系统(独立于 Python)是否支持您尝试设置的语言环境?
窗户
XP.)为什么我的操作系统支持什么语言环境很重要,当我想要的时候
是设置用于输出的编码,输出为
全部
去Emacs,我知道Emacs支持utf8?
(Emacs 22.2.1 i386-mingw-nt5.1.2600.)
达蒙.
根据我的谷歌搜索,我想我需要设置我的语言环境.
不在此操作系统上.在 Windows 上,您需要更改
你的控制台.如果是 cmd.exe 风格的控制台,请使用 chcp.
对于 IDLE,不支持更改输出编码.
如果要输出到文件中,请使用 codecs.open.
如果您绝对想将UTF-8输出到终端,甚至
虽然终端将无法正确渲染,
使用
sys.stdout = codecs.getwriter("UTF-8")(sys.stdout)
HTH,
马丁
问题描述
I''d like to print out a unicode string.
I''m running Python inside Emacs, which understands utf-8, so I want to
force Python to send utf-8 to sys.stdout.
From what I''ve googled, I think I need to set my locale. I don''t
understand how.
import locale
print locale.getlocale()
--(None,None)
print locale.getdefaultlocal()
--(''en_GB'',''cp1252'')
print locale.normalize(''en_GB.utf-8'')
--en_GB.UTF8
locale.setlocale(locale.LC_ALL,''en_GB.UTF8'')
-- locale.Error: unsupported locale setting
I''d be grateful for advice.
Damon.
推荐答案
da**********@gmail.com writes:
From what I''ve googled, I think I need to set my locale. I don''tJust because the locale library knows the normalised name for it
understand how.
import locale
print locale.getlocale()
--(None,None)
print locale.getdefaultlocal()
--(''en_GB'',''cp1252'')
print locale.normalize(''en_GB.utf-8'')
--en_GB.UTF8
locale.setlocale(locale.LC_ALL,''en_GB.UTF8'')
-- locale.Error: unsupported locale setting
I''d be grateful for advice.
doesn''t mean it''s available on your OS. Have you confirmed that your
OS (independent of Python) supports the locale you''re trying to set?
--
\ "Oh, I love your magazine. My favorite section is ''How To |
`\ Increase Your Word Power''. That thing is really, really, |
_o__) really... good." -- Homer, _The Simpsons_ |
Ben Finney
On Apr 19, 12:51 am, Ben Finney wrote:Just because the locale library knows the normalised name for itNo. How do I found out which locales my OS supports? (I''m running
doesn''t mean it''s available on your OS. Have you confirmed that your
OS (independent of Python) supports the locale you''re trying to set?
Windows
XP.) Why does it matter what locales my OS supports, when all I want
is to set the encoding to be used for the output, and the output is
all
going to Emacs, and I know that Emacs supports utf8?
(Emacs 22.2.1 i386-mingw-nt5.1.2600.)
Damon.
From what I''ve googled, I think I need to set my locale.
Not on this operating system. On Windows, you need to change
your console. If it is a cmd.exe-style console, use chcp.
For IDLE, changing the output encoding is not supported.
If you want to output into a file, use codecs.open.
If you absolutely want to output UTF-8 to the terminal even
though the terminal will not be able to render it correctly,
use
sys.stdout = codecs.getwriter("UTF-8")(sys.stdout)
HTH,
Martin