Python 2.7 的语法记录

2017-04-14  本文已影响14人  研究路上

Python 2.7 的语法记录

字符串

  1. 格式化字符串

    1. 采用百分号

      For example,

      location = "Zhongguancun"
      city = "Beijing"
      "I am a Ph.D from %s, %s" % (location, city)
      
    2. 采用 .format 属性函数

      For example,

      location = "Zhongguancun"
      city = "Beijing"
      "I am a Ph.D from {0}, {1}".format(location, city)
      
    3. 采用 + 将字符串连接在一起

      For example,

      location = "Zhongguancun"
      city = "Beijing"
      "I am a Ph.D from " + str(location) + "," + str(city)
      

常用模块

  1. getopt

    1. 主要参考了博客

    2. getopt 模块中的函数 getopt

      # name: test.py
      # getopt(args, shortopts, longopts=[])
      """
      # 参数 args 一般是 sys.argv[1:]
      # shortopts 短格式 (-)
      # longopts 长格式 (--)
      """
      
      # 参考代码
      try:
          options, args = getopt.getopt(sys.argv[1:], "hp:i:", ["help", "ip=", "port="])
          # 第二个参数是短格式
            # h 代表短格式 -h,没有冒号代表后面不带参数
              # p 代表短格式 -p,i 代表短格式 -i,p 和 i 后带有冒号代表这两个短格式后需要带参数
          # 第三个参数是长格式
            # help 代表长格式 --help, 没有等号代表后面不带参数
              # ip 和 port 代表长格式 --ip, --port, 带有等号代表这两个长格式后需要带参数
          # 第一个返回值 options 是个包含元组的列表,每个元组是分析出来的每个包含 '-' or '--' 参数的格式信息,例如:[('-i', '127.0.0.1'), ('-p', '80')]
          # 第二个返回值 args 是个列表,包含那些没有 '-' or '--'的参数 ,例如:['55', '66']
          # 注意到这里解析的参数的值都是字符串...
      except getopt.GetoptError:
          sys.exit(1)
      for name, value in options:
          if name in ("-h", "--help"):
              # output some help doc
          if name in ("-i", "--ip"):
              print("ip is " + str(value))
          if name in ("-p", "--port"):
              print("port is " + str(value))
      

      shell 如下来调用上述代码:

      python test.py -i 127.0.0.1 -p 80 55 66
      
  2. pickle

    1. 主要参考了 python 官方文档的信息聚合

    2. 函数load

      • 这里主要介绍一下从一个pickle文件中读取二进制信息,并转换为 python object

        import pickle
        with open('test.pkl', 'rb') as rfile:
            py_obj = pickle.load(rfile)
        
    3. 函数dump

      • 这里主要介绍一下将 python object 写入 pickle文件

        import pickle
        with open('test.pkl', 'wb') as wfile:
            pickle.dump(py_obj, wfile)
        
  3. json

    1. 主要参考了 python 官方文档的信息聚合

      • 这里推荐一下这个 多种语言和模块的API信息聚合网站(devdocs.io),用户用chrome打开之后,可以选择自己的preference,然后可以将自己常用语言或模块(例如 python, tensorflow, scikit-learn等)的API信息 install,这样保证在没有网的时候也可以使用
    2. 函数 load

      • 这里主要介绍一下从一个json文件中如何读取出python object

        import json
        with open('test.json', 'rb') as rfile:
            py_obj1 = json.load(rfile)
        
    3. 函数 dump

      • 这里主要介绍一下将python object写入一个json文件

        import json
        py_obj2 = {"a":3, "b":2, "c":1}
        with open('test.json', 'wb') as wfile:
            json.dump(py_obj2, wfile)
        
    4. 函数 loads 和 dumps 和上述函数有微小区别,主要将json与普通字符串进行转换?

      • 这里描述一种 从json文件中读取 python object的方式

        import json
        # 从 json文件中读取 json 字符串
        with open('test.json', 'rb') as rfile:
            json_str2 = rfile.read()
        py_obj2 = json.loads(json_str2)
        
      • 这里描述一种 将 python object 写入 json文件的方式

        import json
        py_obj1 = {"a":1, "b":2, "c":3}
        # 将 py_obj 转化为 json 字符串
        json_str1 = json.dumps(py_obj1)
        # 将 json字符串写入json 文件
        with open('test.json', 'wb') as wfile:
            wfile.write(json_str1)
        
上一篇 下一篇

猜你喜欢

热点阅读