需要把一段py2的**轉為py3,執行後,遇到乙個錯誤就解決乙個錯誤,一般都會有替換方案。
1. syntaxerror: missing parentheses in call to 'print'
所有「print x」 更改為「print
(x)」
2. 報錯:typeerror:
'dict_keys'
object
isnot 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
isnot subscriptable
解決方案:
in python 3
,map returns an iterable object of type
map,
andnot a subscriptible list
, which would allow you to write map
[i]. to force a list result, write
payintlist =
list
(map
(int
,paylist)
)
學習Python 2還是Python 3?
學習python 2還是python 3?答案 都要學!是否有如下疑問 python 3 才是python的未來。python 官方都建議指直接學習python 3。python 2 只維護到2020年。python 2慢慢的就沒人用了。那麼我們來 下 python 2和 python 3 放入區別...
python2版本程式轉python3版本
碰見有些python程式是2.7版本的,發現程式中print 函式和xrange 函式需要改變,不想乙個乙個的改,所以寫了乙個程式來更改。import os file1 r 需要更改的程式的路徑 file2 asd.txt with open file1,r encoding utf 8 as f ...
python3和python2的區別
1.效能 py3.0執行 pystone benchmark的速度比py2.5慢30 guido認為py3.0有極大的優化空間,在字串和整形操作上可 以取得很好的優化結果。py3.1效能比py2.5慢15 還有很大的提公升空間。2.編碼 py3.x原始碼檔案預設使用utf 8編碼,這就使得以下 是合...