首页 > 编程语言 >将python2.7项目转为Python3问题记录

将python2.7项目转为Python3问题记录

时间:2022-10-25 14:57:57浏览次数:40  
标签:struct int self python2.7 报错 转为 into Python3 pack

将一个Python2.7开发的测试工具项目转化为Python3。

工具:Python自带的2to3.py

将所有.py文件进行转化,生成的python3文件为原文件名,python2文件在后面加.bak

 

1. 代码如:b = str(a).strip("\0").decode('gbk') + ','

运行报错,显示“AttributeError: 'str' object has no attribute 'decode'”。

参考:https://blog.csdn.net/zz00008888/article/details/123383157

修改为:b = a.decode('gbk').strip("\0") + ','

 

2. 代码如:struct.pack_into('16s', self.buffer_, self.offset_, bufferdict['name'])

报错:python3 struct.pack方法报错argument for 's' must be a bytes object

参考:https://blog.csdn.net/weixin_38383877/article/details/81100192

修改为:

if 's' in self.format_:

  struct.pack_into(self.format_, self.buffer_, self.offset_, bufferdict['name'].encode('utf-8'))

else:

  struct.pack_into(self.format_, self.buffer_, self.offset_, bufferdict['name']

 

3. 代码如:resultlist.append(int(inputBuffer[i].encode('hex'), 16))

报错:python出错:AttributeError: 'int' object has no attribute 'encode'

修改为:resultlist.append(int(hex(inputBuffer[i]), 16))

 

标签:struct,int,self,python2.7,报错,转为,into,Python3,pack
From: https://www.cnblogs.com/workingdiary/p/16813173.html

相关文章