UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe6 in position 0: ordinal not in range(128)
生活随笔
收集整理的這篇文章主要介紹了
UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe6 in position 0: ordinal not in range(128)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
詳細錯誤如下
代碼如下
# -*- coding: utf-8 -*-
import xlwt
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
# 創(chuàng)建新的工作簿
table = xlwt.Workbook()
# 添加工作表
sheet = table.add_sheet("Sheet")
# 向表中寫入數(shù)據(jù)
sheet.write(0, 0, "num")
sheet.write(0, 1, "name")
sheet.write(0, 2, "age")
sheet.write(1, 0, 1)
sheet.write(1, 1, "李白")
sheet.write(1, 2, 108)
# 保存工作簿到指定的目錄
table.save("/home/hly/hly/sample.xls")
在python 3 中沒有問題, 在python 2 中使用報錯了
定位是是寫如中文的問題引起的
自己的處理方法是在中文前面添加了一個u 這樣就不報錯了,如下
# -*- coding: utf-8 -*-
import xlwt
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
# 創(chuàng)建新的工作簿
table = xlwt.Workbook()
# 添加工作表
sheet = table.add_sheet("Sheet")
# 向表中寫入數(shù)據(jù)
sheet.write(0, 0, "num")
sheet.write(0, 1, "name")
sheet.write(0, 2, "age")
sheet.write(1, 0, 1)
sheet.write(1, 1, u"李白")
sheet.write(1, 2, 108)
# 保存工作簿到指定的目錄
table.save("/home/hly/hly/sample.xls")
?
總結
以上是生活随笔為你收集整理的UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe6 in position 0: ordinal not in range(128)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何提高阅读速度的方法和原理?
- 下一篇: python 十进制转二进制,十进制转八