小编dan*_*iel的帖子

打印utf-8编码的字符串

我正在使用BeautifulSoup从HTML中提取一些文本,但我无法弄清楚如何将其正确打印到屏幕上(或者就此而言的文件).

这是我的包含文本的类的样子:

class Thread(object):
    def __init__(self, title, author, date, content = u""):
        self.title = title
        self.author = author
        self.date = date
        self.content = content
        self.replies = []

    def __unicode__(self):
        s = u""

        for k, v in self.__dict__.items():
            s += u"%s = %s " % (k, v)

        return s

    def __repr__(self):
        return repr(unicode(self))

    __str__ = __repr__
Run Code Online (Sandbox Code Playgroud)

当我尝试打印Thread这里的实例时,我在控制台上看到的是:

~/python-tests $ python test.py
u'date = 21:01 03/02/11 content =  author = \u05d3"\u05e8 \u05d9\u05d5\u05e0\u05d9 \u05e1\u05d8\u05d0\u05e0\u05e6\'\u05e1\u05e7\u05d5 replies = [] title = \u05de\u05d1\u05e0\u05d4 \u05d4\u05de\u05d1\u05d7\u05df …
Run Code Online (Sandbox Code Playgroud)

python unicode encoding

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

标签 统计

encoding ×1

python ×1

unicode ×1