2006年04月30日 星期日 18:34
我想把 Dive into Python 中文版的 html 由 utf-8 转换成 gb2312 编码,做了
个小程序。但是
不知道为什么有错误,大家帮忙看看。 谢谢。
错误如下:
Traceback (most recent call last):
File "R:\unicode2gb.py", line 23, in ?
utf2gb(r'r:\test')
File "R:\unicode2gb.py", line 16, in utf2gb
gb_text = unicode_text.encode('gb2312')
UnicodeEncodeError: 'gb2312' codec can't encode character u'\xa0' in
position 197: illegal multibyte sequence
源程序:
#! -*- coding=utf-8 -*-
import os
def utf2gb(htmlpath):
for root, dirs, files in os.walk(htmlpath):
for filename in files:
if filename.split('.')[-1] != 'html': continue
filepath= '\\'.join([root,filename])
f = open(filepath, 'r')
utf_text = ''.join(f.readlines())
f.close()
unicode_text = unicode(utf_text,'utf-8')
gb_text = unicode_text.encode('gb2312')
f = open(filepath, 'w')
f.write(utf_text)
f.close()
if __name__ == '__main__':
utf2gb(r'r:\test')
2006年04月30日 星期日 21:27
你是不是发邮件频率过快?/ 你的几乎所有邮件都在我垃圾邮件项中 在06-4-30,tocer <tocer.deng at gmail.com> 写道: > > 我想把 Dive into Python 中文版的 html 由 utf-8 转换成 gb2312 编码,做了 > 个小程序。但是 > 不知道为什么有错误,大家帮忙看看。 谢谢。 > > 错误如下: > Traceback (most recent call last): > File "R:\unicode2gb.py", line 23, in ? > utf2gb(r'r:\test') > File "R:\unicode2gb.py", line 16, in utf2gb > gb_text = unicode_text.encode('gb2312') > UnicodeEncodeError: 'gb2312' codec can't encode character u'\xa0' in > position 197: illegal multibyte sequence > > 源程序: > > #! -*- coding=utf-8 -*- > > import os > > def utf2gb(htmlpath): > for root, dirs, files in os.walk(htmlpath): > for filename in files: > if filename.split('.')[-1] != 'html': continue > filepath= '\\'.join([root,filename]) > f = open(filepath, 'r') > utf_text = ''.join(f.readlines()) > f.close() > unicode_text = unicode(utf_text,'utf-8') > gb_text = unicode_text.encode('gb2312') > f = open(filepath, 'w') > f.write(utf_text) > f.close() > > if __name__ == '__main__': > utf2gb(r'r:\test') > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060430/4c7d4db3/attachment.htm
2006年04月30日 星期日 21:53
嗯,偶的也是。用gmail收邮件列表,楼主的帖子都在垃圾邮件里面。 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060430/706d12bf/attachment.html
Zeuux © 2025
京ICP备05028076号