Python 2.4 vs. Python 3.0 One, the print from the statement into a function Original: Print 1, 2+3 Instead: Print (1, 2+3) Two, range and xrange Original: range (0, 4) result is list [0,1,2,3] Instead:list (range (0,4)) Original: xrange (0, 4) variable control for the For Loop Instead:range (0,4) Three, string Original: string stored in 8-bit string Instead: string is stored in 16-bit Unicode string Iv. changes to the try except statement Original: try: ...... Except Exception, E: ...... Switch try: ...... Except Exception as e: ...... V. Open File Original: file (...) or open (...) Switch only Open (...) Six, input a string from the keyboard Original: raw_input ("hint message") Instead: input ("hint message") Vii.. Bytes data types A Bytes Object is an immutable array. The items is 8-bit bytes, represented by integers in the range 0 <= x < 256. Bytes can be thought of as "byte array" objects, each element is a 8-bit byte, and the value range 0~255. Since strings are stored in Unicode encoding in Python 3.0, when a binary file is written, the string cannot be written directly (or read) and must be encoded in some way into a sequence of bytes before it can be written. (a) string encoding (encode) is bytes Example: s = "Zhang San abc12" b = s.encode (encoded) # b is the bytes type of data # Common encoding methods are: "Uft-16", "Utf-8", "GBK", "gb2312", "ASCII", "latin1" and so on. # Note: An exception is thrown when a string cannot be encoded as a specified "encoding method" (ii) bytes decoding (decode) as a string s = "Zhang San abc12" b = S.encode ("GBK") # string s encoded as a sequence of bytes in GBK format S1 = B.decode ("GBK") # decodes byte sequence B into a string in GBK format # description, an exception is thrown when a sequence of bytes cannot be decoded in the specified encoding format (iii) Examples of using methods #coding =GBK f = open ("C:\\1234.txt", "WB") s = "Dick and Harry Abcd1234" # ------------------------------- # in python2.4 we can write this: # F.write (s) # but throws an exception in Python 3.0 # ------------------------------- b = S.encode ("GBK") F.write (b) F.close () Input ("?") An example of reading this file: #coding =GBK f = open ("C:\\1234.txt", "RB") F.seek (0,2) #定位至文件尾 n = F.tell () #读取文件的字节数 F.seek (0,0) #重新定位至文件开始处 b = F.read (n) # ------------------------------ # in Python 2.4 b is a string type # to Python 3.0, B is the bytes type # Therefore, you need to specify the encoding method to confirm the code # ------------------------------ s = B.decode ("GBK") Print (s) # ------------------------------ # in Python 2.4 You can write print s or print (s) # to Python 3.0 must write print (s) # ------------------------------ F.close () Input ("?") Should be displayed after running: Dick and Harry Abcd1234 (iv) bytes sequence, one but formed, whose contents are immutable Cases: S= "ABCD" B=s.encode ("GBK") Print B[0] # Display B[0] = 66 # Execute the sentence, an exception occurred: ' bytes ' object does not the support item assignment Viii. chr (K) and Ord (c) Python 2.4.2 before chr (k) converts the encoded k to characters, the range of K is 0 ~ 255 Ord (c) takes a single character encoding, the range of return values: 0 ~ 255 Python 3.0 chr (k) converts the encoded k to characters, the range of K is 0 ~ 65535 Ord (c) takes a single character encoding, the range of return values: 0 ~ 65535 Nine, division operator Python 2.4.2 before 10/3 Results of 3 Python 3.0 10/3 results for 3.3333333333333335 //3 results for 3 Ten, byte array object---New (i) initialization a = ByteArray (Ten) # A is an array of 10 bytes, each of which is a byte, and the type borrows an int # at this point, each element has an initial value of 0 (b) The byte array is mutable a = ByteArray (Ten) a[0] = # You can change its elements with an assignment statement, but the assigned value must be between 0 and 255 (c) A slice of a byte array is still an array of bytes (iv) Converting a string into a byte array #coding =GBK s = "Hello" b = S.encode ("GBK") # first converts a string into bytes by some sort of "GBK" encoding C = ByteArray (b) #再将 bytes into a byte array can also write C = bytearray ("Hello", "GBK") (v) byte arrays converted to strings C = ByteArray (4) C[0] = 65; c[1]=66; C[2]= 67; C[3]= 68 s = C.decode ("GBK") Print (s) # should show: ABCD (vi) Byte array can be used to write to a text file #coding =GBK f = open ("C:\\1234.txt", "WB") s = "Dick and Harry Abcd1234" # ------------------------------- # in python2.4 we can write this: # F.write (s) # but throws an exception in Python 3.0 # ------------------------------- b = S.encode ("GBK") F.write (b) C=bytearray ("Harry", "GBK") F.write (c) F.close () Input ("?") |