如何打印unicode字符串? [英] How to print a unicode string?

查看:469
本文介绍了如何打印unicode字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印出一个unicode字符串。


我在Emacs中运行Python,它理解utf-8,所以我想

强制Python将utf-8发送到sys.stdout。


从我用Google搜索,我想我需要设置我的语言环境。我不知道
了解如何。


导入语言环境

print locale.getlocale()

- (无,无)

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:不支持的区域设置


我将不胜感激。

Damon。

解决方案

da **********@gmail.com 写道:


从我用谷歌搜索,我想我需要设置我的语言环境。我不知道
了解如何。


导入语言环境

print locale.getlocale()

- (无,无)

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:不支持的区域设置


我将不胜感激。



只是因为语言环境库知道它的标准化名称

并不意味着它可以在你的操作系统上使用。你确认你的

操作系统(独立于Python)支持你想要设置的语言环境吗?


-

\哦,我爱你的杂志。我最喜欢的部分是'如何|

` \增加你的单词能力''。那件事真的,真的,|

_o__)真的......很好。 - 荷马,_辛普森一家_ |

Ben Finney


4月19日上午12:51,Ben Finney写道:
< blockquote class =post_quotes>
只是因为语言环境库知道它的标准化名称

并不意味着它可以在你的操作系统上使用。你确认你的b $ b操作系统(独立于Python)支持你试图设置的语言环境吗?



否。如何找到我的操作系统支持的语言环境? (我正在运行

Windows

XP。)为什么我操作系统支持的语言环境很重要,当我想要的时候

是设置用于输出的编码,输出是

所有

转到Emacs,我知道Emacs支持utf8?

(Emacs 22.2.1 i386-mingw-nt5.1.2600。)


Damon。


来自我的google,我想我需要设置我的语言环境。


不在此操作系统上。在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''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.

Just because the locale library knows the normalised name for it
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 it
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?

No. How do I found out which locales my OS supports? (I''m running
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


这篇关于如何打印unicode字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆