相关疑难解决方法(0)

在Python中管道stdout时设置正确的编码

管道Python程序的输出时,Python解释器会对编码感到困惑,并将其设置为None.这意味着这样的程序:

# -*- coding: utf-8 -*-
print u"åäö"
Run Code Online (Sandbox Code Playgroud)

正常运行时会正常工作,但失败时:

UnicodeEncodeError:'ascii'编解码器无法对位置0中的字符u'\ xa0'进行编码:序数不在范围内(128)

当在管道序列中使用时.

在配管时使这项工作的最佳方法是什么?我可以告诉它使用shell/filesystem /无论使用什么编码吗?

到目前为止我看到的建议是直接修改你的site.py,或者使用这个hack对defaultencoding进行硬编码:

# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
print u"åäö"
Run Code Online (Sandbox Code Playgroud)

是否有更好的方法使管道工作?

python terminal encoding stdout python-2.x

327
推荐指数
7
解决办法
19万
查看次数

与Python 3.4,Unicode,不同语言和Windows有什么关系?

快乐的例子:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

czech = u'Leoš Janá?ek'.encode("utf-8")
print(czech)

pl = u'Zdzis?aw Beksi?ski'.encode("utf-8")
print(pl)

jp = u'??? ?? ??'.encode("utf-8")
print(jp)

chinese = u'??'.encode("utf-8")
print(chinese)

MIR = u'?????? ??? ?????????? ????????'.encode("utf-8")
print(MIR)

pt = u'Minha Língua Portuguesa: çáà'.encode("utf-8")
print(pt)
Run Code Online (Sandbox Code Playgroud)

不愉快的输出:

b'Leo\xc5\xa1 Jan\xc3\xa1\xc4\x8dek'
b'Zdzis\xc5\x82aw Beksi\xc5\x84ski'
b'\xe3\x83\xaa\xe3\x83\xb3\xe3\x82\xb0 \xe5\xb1\xb1\xe6\x9d\x91 \xe8\xb2\x9e\xe5\xad\x90'
b'\xe4\xba\x94\xe8\xa1\x8c'
b'\xd0\x9c\xd0\xb0\xd1\x88\xd0\xb8\xd0\xbd\xd0\xb0 \xd0\xb4\xd0\xbb\xd1\x8f \xd0\x98\xd0\xbd\xd0\xb6\xd0\xb5\xd0\xbd\xd0\xb5\xd1\x80\xd0\xbd\xd1\x8b\xd1\x85 \xd0\xa0\xd0\xb0\xd1\x81\xd1\x87\xd1\x91\xd1\x82\xd0\xbe\xd0\xb2'
b'Minha L\xc3\xadngua Portuguesa: \xc3\xa7\xc3\xa1\xc3\xa0'
Run Code Online (Sandbox Code Playgroud)

如果我像这样打印它们:

jp = u'??? ?? ??'
print(jp)
Run Code Online (Sandbox Code Playgroud)

我明白了:

Traceback (most recent call last):
  File "x.py", line 5, in <module>
    print(jp)
  File …
Run Code Online (Sandbox Code Playgroud)

python unicode

24
推荐指数
2
解决办法
2万
查看次数

让python在Windows XP上使用控制台以UTF8进行打印

我想在Windows XP上配置我的控制台以支持UTF8并让python检测并使用它.

到目前为止,我的尝试:

C:\Documents and Settings\Philippe>C:\Python25\python.exe
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print u'é'
é
>>> import sys
>>> sys.stdout.encoding
'cp437'
>>> quit()
Run Code Online (Sandbox Code Playgroud)

所以,默认情况下我在cp437并且python检测到就好了.

C:\Documents and Settings\Philippe>chcp 65001
Active code page: 65001

C:\Documents and Settings\Philippe>python
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> …
Run Code Online (Sandbox Code Playgroud)

python windows unicode windows-xp utf-8

11
推荐指数
1
解决办法
6619
查看次数

Python:如何强制"打印"使用__unicode__而不是__str__,或者自然地"打印"消息而不显式调用unicode()

基本上我只是希望能够使用名为Bottle的类创建实例:例如class Bottle(object):... ,然后在另一个模块中能够简单地"打印"任何实例不必破解代码来显式调用字符编码例程.

总之,当我尝试:

obj=Bottle(u"??")
print obj
Run Code Online (Sandbox Code Playgroud)

或者到"就地""打印":

print Bottle(u"??")
Run Code Online (Sandbox Code Playgroud)

我明白了:

"UnicodeEncodeError: 'ascii' codec can't encode characters"
Run Code Online (Sandbox Code Playgroud)

类似的stackoverflow问题:

¢目前切换到python3是不可行的.¢

关于如何进行utf-8打印的解决方案或提示(和解释)(就像U类在下面成功一样)将非常感激.:-)

ThanX N.

-

示例代码:

-------- 8> < - - - - 在这里切 - - - -

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def setdefaultencoding(encoding="utf-8"):
  import sys, codecs

  org_encoding = sys.getdefaultencoding()
  if org_encoding == "ascii": # not good enough
    print "encoding set to "+encoding
    sys.stdout = codecs.getwriter(encoding)(sys.stdout)
    sys.stderr = codecs.getwriter(encoding)(sys.stderr) …
Run Code Online (Sandbox Code Playgroud)

python string unicode character-encoding cjk

7
推荐指数
1
解决办法
3748
查看次数

Python 3,是使用sys.stdout.buffer.write()好风格?

在我学习了如何在Python 3.0 web脚本中阅读 unicode文件之后,现在是时候让我学习使用print()unicode了.

我搜索了编写unicode,例如这个问题解释了你不能将unicode字符写入非unicode控制台.但是,就我而言,输出是给Apache的,我相信它能够处理unicode文本.但是,出于某种原因,stdout我的网络脚本是在ascii.

显然,如果我打开一个文件写自己,我会做类似的事情

open(filename, 'w', encoding='utf8')
Run Code Online (Sandbox Code Playgroud)

但由于我得到了一个开放的流,我使用了

sys.stdout.buffer.write(mytext.encode('utf-8'))
Run Code Online (Sandbox Code Playgroud)

一切似乎都有效.这是否违反某些良好行为规则或有任何意外后果?

unicode cgi python-3.x

6
推荐指数
1
解决办法
5190
查看次数