python2转python3常见错误

需要把一段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))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值