需要把一段py2的代码转为py3,执行后,遇到一个错误就解决一个错误,一般都会有替换方案。
1. SyntaxError: Missing parentheses in call to 'print'
所有“print X” 更改为“print(X)”
2. 报错:TypeError: 'dict_keys' object is not subscriptable
解决:self._G.node.keys()[:] 改为 list(self._G.node.keys())
3. 报错:AttributeError: 'collections.defaultdict' object has no attribute 'itervalues'
解决:m.itervalues() 改为 m.values()
4. 报错:AttributeError: module 'string' has no attribute 'atoi'
解决:v_i = string.atoi(s) 改为 v_i = int(s)
5. 报错:TypeError: 'dict_keys' object does not support indexing
解决:将dict_keys()类型转化为list
visit_sequence = self._G.keys(); random.shuffle(visit_sequence) 改为 visit_sequence = list(self._G.keys()); random.shuffle(visit_sequence)
6. csv.write()报错: 中str转成bytes类型后用csv.writerow()写入csv文件仍然出错
根本原因是Python版本问题python2.x中要求用‘wb’,python3.x中要求用'w'
7. map对象报错:Python map object is not subscriptable
解决方案:
In Python 3, map returns an iterable object of type map, and not a subscriptible list, which would allow you to write map[i]. To force a list result, write
payIntList = list(map(int,payList))
python2转python3常见错误
最新推荐文章于 2022-04-18 10:36:10 发布