您尚未指定问题,因此这是一个完整的猜测。
strip_tags()功能返回了什么内容?它是返回一个unicode对象,还是一个字节串?如果是后者,当您尝试将其写入文件时,可能会导致解码问题。例如,如果strip_tags()返回utf-8编码的字节字符串:
>>> s = u'This is \xe4 test\nHere is \xe4nother line.'
>>> print s
This is ä test
Here is änother line.
>>> s_utf8 = s.encode('utf-8')
>>> f=codecs.open('test', 'w', encoding='utf8')
>>> f.write(s) # no problem with this... s is unicode, but
>>> f.write(s_utf8)
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib64/python2.7/codecs.py", line 691, in write
return self.writer.write(data)
File "/usr/lib64/python2.7/codecs.py", line 351, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 8: ordinal not in range(128)
如果这是你所看到的,你需要确保在fid.write(result)中传递unicode,这可能意味着确保strip_tags()返回unicode。
此外,我还注意到了其他一些事情:
如果无法打开文件,
codecs.open()将引发IOError异常。它不会返回None,因此if not fid:测试无效。您需要使用try/except,理想情况下使用with。
try:
with codecs.open(htmlFile, "r", encoding = "utf-8") as fid:
htmlText = fid.read()
except IOError, e:
# handle error
print e
并且,您从通过codecs.open()打开的文件中读取的数据将自动转换为unicode,因此调用unicode(htmlText)无法实现任何目标。