Python 2.7 当包含中文字符时返回UnicodeError
我想把aaa.xls(一个包含汉字的文件)翻译成bbb.html,但是Python 2.7 当包含中文字符时返回UnicodeError,python-2.7,pandas,Python 2.7,Pandas,我想把aaa.xls(一个包含汉字的文件)翻译成bbb.html,但是翻译成_html报告unicodeincodeerror。我怎样才能修好它 代码: /#coding: utf-8 import pandas def xls2html(): df = pandas.read_excel('aaa.xls') df.to_html('bbb.html',bold_rows=False) if __name__ == '__main__': xls2h
翻译成_html
报告unicodeincodeerror
。我怎样才能修好它
代码:
/#coding: utf-8
import pandas
def xls2html():
df = pandas.read_excel('aaa.xls')
df.to_html('bbb.html',bold_rows=False)
if __name__ == '__main__':
xls2html()
错误:
df.to_html('aa2.html',bold_rows=False)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1608, in to_html
formatter.to_html(classes=classes, notebook=notebook, border=border)
File "C:\Python27\lib\site-packages\pandas\formats\format.py", line 703, in to_html
html_renderer.write_result(f)
File "C:\Python27\lib\site-packages\pandas\formats\format.py", line 1035, in write_result
_put_lines(buf, self.elements)
File "C:\Python27\lib\site-packages\pandas\formats\format.py", line 2646, in _put_lines
buf.write('\n'.join(lines))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 109-111: ordinal not in range(128)
谢谢大家
我解决这个问题的方法如下:
# coding: utf-8
import pandas
import sys
reload(sys)
sys.setdefaultencoding('gbk')
def xls2html():
df = pandas.read_excel('aaa.xls',encoding='gbk')
df.to_html('bbb.html',bold_rows=False)
if __name__ == '__main__':
xls2html()
尝试用UTF-8读取Excel文件:
df=pandas。读取\u Excel('aaa.xls',encoding='UTF-8')