descriptor 'decode' requires a 'bytes' object but received a 'NoneType'
记录在使用python过程中踩的坑------
使用xlwt库对excel文件进行保存时报错 descriptor 'decode' requires a 'bytes' object but received a 'NoneType'
log:
Traceback (most recent call last):
File "F:/xxxxxx/util/ExcelUtil.py", line 110, in
excel = write_excel("Outbound_Template.xls", 4, 9, "AT2019110912")
File "F:/xxxxxx/util/ExcelUtil.py", line 48, in write_excel
newWb.save(excel_path)
File "F:\Python\lib\site-packages\xlwt\Workbook.py", line 710, in save
doc.save(filename_or_stream, self.get_biff_data())
File "F:\Python\lib\site-packages\xlwt\Workbook.py", line 667, in get_biff_data
before += self.__all_fonts_num_formats_xf_styles_rec()
File "F:\Python\lib\site-packages\xlwt\Workbook.py", line 570, in __all_fonts_num_formats_xf_styles_rec
return self.__styles.get_biff_data()
File "F:\Python\lib\site-packages\xlwt\Style.py", line 185, in get_biff_data
result += self._all_num_formats()
File "F:\Python\lib\site-packages\xlwt\Style.py", line 209, in _all_num_formats
result += NumberFormatRecord(fmtidx, fmtstr).get()
File "F:\Python\lib\site-packages\xlwt\BIFFRecords.py", line 785, in init
ufmtstr = upack2(fmtstr)
File "F:\Python\lib\site-packages\xlwt\UnicodeUtils.py", line 50, in upack2
us = unicode(s, encoding)
TypeError: descriptor 'decode' requires a 'bytes' object but received a 'NoneType'
1.原文件用MS Excel编辑,后执行程序,可以运行
2.原文件用MS Excel编辑,后执行程序,更新后的文件,再用MS Excel编辑,保存,再执行程序,出现上面的错误
我的代码:
# coding:utf-8
def write_excel(file_name, row, col, value, sheet_name="Template"):
excel_path = Configure.read_config("project_path", "path") + "\\data\\" + file_name
# formatting_info = True,得以保存之前数据的格式
old_wb = open_workbook(excel_path, formatting_info=True)
# 将操作文件对象拷贝,变成可写的workbook对象
new_wb = copy(old_wb)
# 获得sheet的对象
new_ws = new_wb.get_sheet(sheet_name)
# 写入数据
new_ws.write(row, col, value)
# 另存为excel文件,并将文件命名
new_wb.save(excel_path)
原因:
用xlutils.copy 得到的excel文件,编码格式变了。然后用Office365或WPS去编辑再保存,得到的文件,decode会是None,xlwt save时会出错。(UnicodeUtils.py中的upack2没有考虑到会有None的情况,网上有的解决方式是自己在源码里加多判断None的情况)
源码:
def upack2(s, encoding='ascii'):
# If not unicode, make it so.
if isinstance(s, unicode_type):
us = s
else:
us = unicode(s, encoding)
# Limit is based on number of content characters
# (not on number of bytes in packed result)
len_us = len(us)
if len_us > 32767:
raise Exception('String longer than 32767 characters')
try:
encs = us.encode('latin1')
# Success here means all chars are in U+0000 to U+00FF
# inclusive, meaning that we can use "compressed format".
flag = 0
n_items = len_us
except UnicodeEncodeError:
encs = us.encode('utf_16_le')
flag = 1
n_items = len(encs) // 2
# n_items is the number of "double byte characters" i.e. MS C wchars
# Can't use len(us).
# len(u"\U0001D400") -> 1 on a wide-unicode build
# and 2 on a narrow-unicode build.
# We need n_items == 2 in this case.
return pack('<HB', n_items, flag) + encs
解决方法:用OpenOffice去编辑文件保存就不会有问题
descriptor 'decode' requires a 'bytes' object but received a 'NoneType'的更多相关文章
- DefaultSerializer requires a Serializable payload but received an object of type [model.Admin]
一.问题描述: 在用redis做二级缓存时,出现如下异常 DefaultSerializer requires a Serializable payload but received an o ...
- Caused by: java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [VCodeModel]
2019-08-20 17:53:24,054 [ERROR] [http-nio-8047-exec-1] [HttpResult.java : 143] 系统异常 org.springframew ...
- ERROR 程序出错,错误原因:'bytes' object has no attribute 'read'
使用json解析数据时,通常遇到这里就会出现问题'bytes' object has no attribute 'read',这是由于使用的json内置函数不同,一个是load另一个是loads. i ...
- Python之scrapy框架之post传输数据错误:TypeError: to_bytes must receive a unicode, str or bytes object, got int
错误名:TypeError: to_bytes must receive a unicode, str or bytes object, got int 错误翻译:类型错误:to_bytes必须接收u ...
- python struct.pack方法报错argument for 's' must be a bytes object 解决
参考 https://blog.csdn.net/weixin_38383877/article/details/81100192 在python3下使用struct模块代码 fileHead = s ...
- AttributeError: 'bytes' object has no attribute 'hex'
python3.5之前bytes数据没有hex()属性 需要使用 ''.join(map(lambda x:('' if len(hex(x))>=4 else '/x0')+hex(x)[2: ...
- 记录Python类与继承的一个错误
今天在学python的类与继承的时候遇到一个错误,原来是自己在ctrl+c ctrl+v的时候漏了一个括号 class Car(): def __init__(self,make,year,mode ...
- 在python中对元祖进行排序
在python里你可以对一个元组进行排序.例子是最好的说明: >>> items = [(1, 'B'), (1, 'A'), (2, 'A'), (0, 'B'), (0, 'a' ...
- __getattr__在python2.x与python3.x中的区别及其对属性截取与代理类的影响
python2.x中的新类型类(New-style class)与python3.x的类一致,均继承object类,而不继承object的类称为经典类(classic class),而对于这两种类,一 ...
随机推荐
- OOP-面向对象(二)
面向对象三大特征: 封装 继承 多态 -封装:对类中成员属性和方法的保护,控制外界对内部成员的访问,修改,删除等操作 私有的: private 在本类内部可以访问,类的外部不可以访问.(python中 ...
- vue2整个项目中,数据请求显示loading图----------未完成阅读,码
一般项目中,有时候会要求,你在数据请求的时候显示一张gif图片,然后数据加载完后,消失.这个,一般只需要在封装的axios中写入js事件即可.当然,我们首先需要在app.vue中,加入此图片.如下: ...
- hdu1251 hash或者字典树
题意: 统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量 ...
- Android so加固的简单脱壳
本文博客地址:http://blog.csdn.net/qq1084283172/article/details/78077603 Android应用的so库文件的加固一直存在,也比较常见,特地花时间 ...
- Python第三章-字符串
第三章 字符串 3.1 基本字符串操作 Python的字符串和元组差不多,是不可以进行改变的,如果想改变值,可以尝试list序列化之后在进行修改. { website = 'http://ww ...
- Python中面向对象和类
目录 面向对象 类的定义 类的访问 类的属性和方法 继承和多态 面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的. 面向对象: 类(C ...
- C++基于文件流和armadillo读取mnist
发现网上大把都是用python读取mnist的,用C++大都是用opencv读取的,但我不怎么用opencv,因此自己摸索了个使用文件流读取mnist的方法,armadillo仅作为储存矩阵的一种方式 ...
- 【maven和jdk】报错:系统找不到指定的文件
创建一个maven项目出错 问题描述 在idea.log出现如下错误(系统找不到指定的文件,但是不知道指定文件是什么) com.intellij.execution.process.ProcessNo ...
- 二、jmeter模拟请求头及监听器之结果树
一.模拟请求头 利用jmeter发送http请求时,被接收的服务端会对发送的该请求进行初步判断,如果不是web端发送的请求就会被打回导致请求不通,这时候需要模拟请求头,模拟正常的用户行为进行发送请求 ...
- Asp.NetCore Web开发之Nlog日志配置
接着讲基于ASP .net Core 的web开发,这节主要讲一下如何使用和配置Nlog进行日志记录. 日志在开发中的作用是很重要的,使用日志,程序出了错误可以及时捕获并记录下来,开发人员可以通过日志 ...