包含中文字符时,to_html返回UnicodeEncodeError

时间:2017-05-06 09:41:03

标签: python-2.7 pandas

我想将aaa.xls(包含中文字符的文件)翻译为bbb.html,但to_html报告UnicodeEncodeError。我该如何解决?

代码:

/#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)

1 个答案:

答案 0 :(得分:0)

谢谢大家

我解决了这个问题如下:

let dataSource = RxTableViewSectionedReloadDataSource<SectionOfCustomData>()
...
dataSource.configureCell = { [weak self] (dataSource, tableView, indexPath, row) -> UITableViewCell in

  switch dataSource[indexPath] {
  case .customData(let customData):
    let cell: CustomDataCell = // dequeue cell
    self?.configure(cell: cell, with: customData)
    return cell

  case .string(let string):
    let cell: StringCell = // dequeue cell
    self?.configure(cell: cell, with: string)
    return cell
  }
}