【发布时间】:2011-07-09 08:30:43
【问题描述】:
我正在使用 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__
当尝试打印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 '
无论我尝试什么,我都无法得到我想要的输出(上面的文本应该是希伯来语)。我的最终目标是将Thread 序列化为文件(使用 json 或 pickle)并能够将其读回。
我在 Ubuntu 10.10 上使用 Python 2.6.6 运行它。
【问题讨论】:
-
前几天我在尝试使用 csv 模块写入 CSV 文档时遇到了类似的问题。皮塔饼。但是
encode()是要走的路。 -
我也遇到了同样的问题,但这完全是因为我的 mysql 连接上没有 'charset=utf8' !