Python2 collection problems encountered transcoding python3

Do python version of the judgment in the proceedings

sys.version_info

# sys.version_info(major=2, minor=7, micro=16, releaselevel='final', serial=0)

sys.version_info.major

# 2

Code Example:

import sys
if sys.version_info.major == 2:
    reload(sys)
    sys.setdefaultencoding('utf-8') 
 
 
if sys.version_info.major == 2:
    a = raw_input("请输入:\n>>")
else:
    a = input("请输入:\n>>")

If a change occurs compatible process of encoding cmd "Chinese garbled characters in the cmd window," suggesting that as unicode:

Right-click cmd window, point the default.

The default code page in the options, select 936.

Links: https://www.cnblogs.com/sunshuhai/p/6242275.html

Py2 need some code into py3, after execution encountered an error to solve a mistake, there are usually alternatives.

Landlord encountered as follows:

"Print X" is changed to "print (X)"
otherwise an error: SyntaxError: Missing parentheses in call to 'print'.

xrange read range
python3 cancel the range function, and the function xrange rename range, so now you can directly use the range function.

m.itervalues () instead m.values ()
otherwise an error: AttributeError: 'collections.defaultdict' object has no attribute 'itervalues'

import cv error

OpenCV is based on C / C ++ is, "cv" and "cv2" represents the difference between the C API and the bottom of the C ++ API, "cv2" indication is ++ C the API. 

Python -3 after version 0.05 was no cv, and where you can call directly cv2 cv function, you can try: 

Import cv2 

Import cv2 AS cv 

and cv should replace all the functions cv2 function, such as: 

cv. LoadImage - "cv2.imread 
cv.SaveImage -" cv2.imwrite

File 'rb' Open given
source code: with open ( 'a.csv', 'rb') as csvfile.

报错:_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

The reason: Because this is not a csv file binary file is simply a text file.

Using 'rb' read in binary bits, bytes read will not be converted into character; mode at rt, Python will automatically \ r \ n converted into text when reading \ n-;

Solution: changed to 'rt' Open.

Error: AttributeError: module 'string' has no attribute 'atoi'
solution: v_i = string.atoi (s) to v_i = int (s) 

Error: TypeError: 'dict_keys' object does not support indexing
resolve: to convert dict_keys () type list

visit_sequence = self._G.keys(); random.shuffle(visit_sequence) 改为 visit_sequence = list(self._G.keys()); random.shuffle(visit_sequence)

Error: csv.write () after the bytes in str converted to type with csv.writerow () writes csv file is still wrong
root cause of the problem is the Python version required by python2.x 'wb', python3.x required with 'w '

 Error: Python map object is not subscriptable ( map object is not available under the standard index)
Solution:

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))
However, in many cases, you can write out your code way nicer by not using indices. For example, with list comprehensions:

payIntList = [pi + 1000 for pi in payList]
for pi in payIntList:
print(pi)
报错:TypeError: 'dict_keys' object is not subscriptable
解决:self._G.node.keys()[:] 改为 list(self._G.node.keys())

In the Python 3+, __ builtin__ module is named the builtins
Python3 reload the need to import from the imp, sys.setdefaultencoding deleted
python2.7:

reload(sys)

sys.setdefaultencoding('utf-8')

python3:

from imp import reload
reload(sys)

sys.setdefaultencoding ( 'utf-8') are deleted, being given

Error: ValueError: can not have unbuffered text  I / O
 error codes are:

fd = open(txt_file, "a+", buffering=0)

"Can not have unbuffered text" The reason is: the buffer is an optional integer that sets the buffer strategy. Close by the buffer 0 (only allowed in binary mode)

Error correction mode as follows:

fd = open(WORDLIST_FILENAME, 'r'),或

fd = open(WORDLIST_FILENAME, 'rb', 0) 

python2.7 : 

reload (SYS) 

sys.setdefaultencoding ( ' UTF-. 8 ' ) 

to python3: 

from IMP Import reload 
reload (SYS) 

sys.setdefaultencoding ( ' UTF-. 8 ' ) are deleted, being given

Error: ValueError: can not have unbuffered text  I / O
 error codes are:

fd = open(txt_file, "a+", buffering=0)

"Can not have unbuffered text" The reason is: the buffer is an optional integer that sets the buffer strategy. Close by the buffer 0 (only allowed in binary mode)

Error correction mode as follows:

fd = open(WORDLIST_FILENAME, 'r'),或

fd = open(WORDLIST_FILENAME, 'rb', 0) 

Guess you like

Origin www.cnblogs.com/blogpro/p/11343784.html