zoukankan      html  css  js  c++  java
  • Python

    Python Module

    1. 基本概念

    • 一个.py文件
    • 在其他模块中进行重用

    2. 引用module的方法

    • import "module name"
      • 使用内部module 
        import sys
        print('The command line arguments are:')
        for i in sys.argv:
            print(i)
        print('
        
        The PYTHONPATH is', sys.path, '
        ')
        
        
        # run command "python test2/f1_module_using_sys.py we are arguments"
        View Code
      • 使用自定义module,且module文件和引用文件在同一目录下
        # module_my.py
        def sayhi():
            print('Hi, this is mymodule speaking.')
        
        version = '0.1'
        View Code
        # module_use_my_module.py
        import f2_module_my
        
        f2_module_my.sayhi()
        print(f2_module_my.version)
        View Code
      • 使用自定义module,且module文件在一个包下面,这个包和引用文件在同一目录下,例如project是根目录,根目录下有引用文件(test文件)和一个包page,包文件下有会被引用的module文件(包下面必须有__init__.py文件,空的就行)。引用import page.module_name
    • from "module name" import "module class or module function"
      • #BasePage is a class in base_page
        from page.base_page import BasePage
        View Code

    3. 引用module时遇到的问题

    • 用pycharm写代码时,使用包下面的module时会报错说找不到对应的module >>> 右键包-Mark Directory as - Source Root

    Python中操作Excel

    使用第三方python库openpyxl

    pip install openpyxl

    # 引入库
    from openpyxl import load_workbook
    
    # 加载一个excel文件,进入到工作簿
    wb = load_workbook("testdata.xlsx")
    
    # 获取指定的表单
    sh = wb["Sheet1"]
    
    # 获取表单中数据
    name = sh.cell(row=2, column=1).value
    print(name)
    
    # 修改表单中的数据--修改数据,注意在写操作时,必须先关闭excel文件
    sh.cell(row=2, column=3).value = 18
    
    # 保存修改数据的操作--保存数据
    wb.save("testdata.xlsx")
    
    # 获取总行数、总列数
    print(sh.max_row)
    print(sh.max_column)
    print(sh.cell(row=3, column=3).value)
    sh.cell(row=3, column=3).value = 25
    print(sh.cell(row=3, column=3))
    
    # 读取所有数据,按行读取
    for index in range(1, sh.max_row + 1):
        print("行号: ", index)
        for sub_i in range(1, sh.max_column + 1):
            print("列号: ", sub_i, "内容: ", sh.cell(row=index, column=sub_i).value)
    View Code

     Python包管理工具-pip

    1. pip install xx

    2. 可以把需要安装的包写在一个文件里面requirements.txt, 然后一起安装 pip install -r requirements.txt

    Python编码

    本来期望结果输出的是中文,结果来一段像这样xe4xbdxa0xe5xa5xbd像是乱码的字符串,就要进行解码decode

    如果输出是一串乱码,就要先编码encode,然后在进行解码

    u = "中文"
    
    str1 = u.encode('gb2312')  # 以gb2312编码对u进行编码,获得bytes类型对象
    print(str1)   # b'xd6xd0xcexc4'
    
    str2 = u.encode('gbk')   # 以gbk编码对u进行编码,获得bytes类型对象
    print(str2)   # b'xd6xd0xcexc4'
    
    str3 = u.encode('utf-8')  #以utf-8编码对u进行编码,获得bytes类型对象
    print(str3)   # b'xe4xb8xadxe6x96x87'
    
    u3 = str3.decode('utf-8')
    print(u3)
    
    u1 = str1.decode('utf-8')
    print(u1)   # UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte

    Python获取命令行参数

     

    import sys
    print('The command line arguments are:')
    for i in sys.argv:
        print(i)   # 第一个参数是文件的路径
    
    # run command "python test2/f1_module_using_sys.py we are arguments"
    # results: >>>>>>
    # pythonBasicLearnf1_module_using_sys.py
    # we
    # are
    # argument

    Python问题

    1. 动态获取module里面的属性  self.request_path = getattr(path, attr)

    2. list转化成逗号相连的字符串  ",".join(map(str, leads_id))

    3. json(字符串)转化成字典  josn.loads(str)    '{"key": "value"}'->{"key": "value"}

       字典转化成json(字符串) json.dumps(dict) 

     
  • 相关阅读:
    HDU1041
    HDU1005
    HDU1231
    MYSQL入门总结
    oracle性能问题排查~记一个单实例的问题
    mysql案例~关于mysql的配置文件个人见解
    数据恢复系列~恢复方案制定
    mysql架构解读~mysql的多源复制
    mysql 案例~select引起的性能问题
    遭遇Bad version number in .class file
  • 原文地址:https://www.cnblogs.com/lj8023wh/p/10456649.html
Copyright © 2011-2022 走看看