Về cơ bản tôi chỉ muốn có thể tạo các cá thể bằng cách sử dụng một lớp có tên là Chai: ví dụ class Bottle(object):...
và sau đó trong một mô-đun khác có thể chỉ đơn giản là "in" bất kỳ trường hợp mà không cần phải hack mã để gọi một cách rõ ràng mã hóa ký tự thường trình.Python: Cách ép "in" để sử dụng __unicode__ thay vì __str__, hoặc nếu không tự nhiên "in" tin nhắn mà không gọi một cách rõ ràng unicode()
Nói tóm lại, khi tôi cố gắng:
obj=Bottle(u"味精")
print obj
Hoặc để một "tại chỗ" "print":
print Bottle(u"味精")
tôi nhận được:
"UnicodeEncodeError: 'ascii' codec can't encode characters"
câu hỏi stackoverflow tương tự:
- unicode class in Python
- how to print chinese word in my code.. using python
- Python string decoding issue
- python 3.0, how to make print() output unicode?
¢ Hiện không khả thi để chuyển sang python3. ¢
Giải pháp hoặc gợi ý (và giải thích) về cách thực hiện in utf-8 tại chỗ (giống như lớp U không thành công bên dưới) sẽ được đánh giá cao. :-)
thanx N
-
đangmẫu:
-------- 8> < - - - - cắt ở đây - - - -
#!/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)
setdefaultencoding()
msg=u"味精" # the message!
class U(unicode): pass
m1=U(msg)
print "A)", m1 # works fine, even with unicode, but
class Bottle(object):
def __init__(self,msg): self.msg=msg
def __repr__(self):
print "debug: __repr__",self.msg
return '{{{'+self.msg+'}}}'
def __unicode__(self):
print "debug: __unicode__",self.msg
return '{{{'+self.msg+'}}}'
def __str__(self):
print "debug: __str__",self.msg
return '{{{'+self.msg+'}}}'
def decode(self,arg): print "debug: decode",self.msg
def encode(self,arg): print "debug: encode",self.msg
def translate(self,arg): print "debug: translate",self.msg
m2=Bottle(msg)
#print "B)", str(m2)
print "C) repr(x):", repr(m2)
print "D) unicode(x):", unicode(m2)
print "E)",m2 # gives: UnicodeEncodeError: 'ascii' codec can't encode characters
-------- 8> < - - - - cắt ở đây - - - - Python 2.4 đầu ra:
encoding set to utf-8
A) 味精
C) repr(x): debug: __repr__ 味精
{{{\u5473\u7cbe}}}
D) unicode(x): debug: __unicode__ 味精
{{{味精}}}
E) debug: __str__ 味精
Traceback (most recent call last):
File "./uc.py", line 43, in ?
print "E)",m2 # gives: UnicodeEncodeError: 'ascii' codec can't encode characters
UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-4: ordinal not in range(128)
-------- 8> < - - - - cắt ở đây - - - - Python 2.6 đầu ra:
encoding set to utf-8
A) 味精
C) repr(x): debug: __repr__ 味精
Traceback (most recent call last):
File "./uc.py", line 41, in <module>
print "C) repr(x):", repr(m2)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-4: ordinal not in range(128)
đầu ra không phản ánh mã, ví dụ: 'repr (x)' sẽ tạo 'UnicodeEncodeError' quá – jfs
@ J.F. Sebastian: Python 2.4 sản xuất ở trên, vì vậy tôi chạy mã trên 2.6 và repr bây giờ cũng tạo ra thông báo lỗi. – NevilleDNZ
Thông báo "UnicodeEncodeError: 'ascii' codec không thể mã hóa các ký tự" khiến tôi nghi ngờ rằng "in" không sử dụng "sys.stdout" vì tôi đã thay đổi codec/mã hóa của tệp này thành "utf-8" với "sys.stdout = codecs.getwriter (encoding) (sys.stdout) " – NevilleDNZ