zoukankan      html  css  js  c++  java
  • Python 2x -> 3.x

      Nowadays, Python 3 is becoming more and more popular than Python 2, but there are still a lot of codes of Python 2 remained. Although we can use them on Python 2 or use pyenv to manage the versions of Python, but I still want to modify these codes to make them runnable in python 3.

      2016/05/31: Hot, 22 - 34 ℃, Light rain, Tuesday

      1.str and bytes(type in python)

      In Python 2, str are given equal treatment to bytes, absolutly. In my opinion, I don't think it is good. String is a explicit entity of bytes, and bytes is inner entity of string.

      We can easily declare a bytes value from a const string. Example: kom_header = b"KOG GC TEAM MASSFILE V.0.3."

      Convert

      str->decode->bytes

        example: s = b'x6dx61x69x6Bx61x7Ax65'

                       s.decode('utf-8')

      bytes->encode->encoding

        example: bytes(str, encoding = encoding_page)

      Attention:

        In Python 3, we should open a binary file in binary mode, instead of text mode.

      2.xrange() and range()

         In Python 2, there is some differences between xrange() and range(), which cause some unfair treatment to range()(QAQ,in perfermance),but now these differences are disappeared in Python 3. We can use range()(in fact, it is xrange()) only in Python 3, xrange() get the formal position to replace range().And range() function has died.(pupu).

        Now range() will make a iterator (on rules) instead of a list.

  • 相关阅读:
    思维导图
    Delphi 之弹出气泡消息提示
    delphi 响应鼠标进入控件消息
    Delphi 获取当前鼠标下的控件内容
    delphi TTBXToolBar 增加外部控件
    delphi button 实现下拉列表
    delphi 设置多屏幕
    电脑双屏改单屏后看不到文件问题的解决
    delphi ListView 设置固定列宽
    数字孪生(Digital twin)
  • 原文地址:https://www.cnblogs.com/maikaze/p/5546619.html
Copyright © 2011-2022 走看看