我正在使用BeautifulSoup从HTML中提取一些文本,但我无法弄清楚如何正确地将其打印到屏幕(或针对该文件)。打印utf-8编码的字符串
这里是我的包含文字类的样子:
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文档时,前几天遇到类似的问题。 PITA。但'encode()'是要走的路。 – 2011-03-05 10:50:26
我有同样的问题,但它是关于我的MySQL连接没有'charset = utf8'! – kommradHomer 2014-07-21 14:59:04