• 2016/09/23


    1.  Configparser

    # configparser用于处理特定格式的文件,其本质上是利用open来操作文件。
    import configparser
    
    config = configparser.ConfigParser()
    config.read('f1',encoding='utf-8')
    
    # 获取所有节点
    ret_1 = config.sections()
    print(ret_1)
    
    # 获取指定节点下所有的键值对
    ret_2 = config.items('section1')
    print(ret_2)
    
    # 获取指定节点下所有的键
    ret_3 = config.options('section1')
    print(ret_3)
    
    # 获取指定节点下指定key的值
    v1 = config.get('section1','k1')
    v2 = config.getint('section4','k4')
    v3 = config.getfloat('section5','k5')
    v4 = config.getboolean('section6','k6')
    print(v1)
    print(v2)
    print(v3)
    print(v4)
    
    # 检查节点
    has_sec = config.has_section('section7')
    print(has_sec)
    
    # 添加节点
    config.add_section('SEC_1')
    config.write(open('f2','w'))
    
    # 删除节点
    config.remove_section('SEC_1')
    config.write(open('f3','w'))
    
    # 检查指定组内的键值对
    has_opt = config.has_option('section1','k1')
    print(has_opt)
    
    # 删除指定组内的键值对
    config.remove_option('section1','k1')
    config.write(open('f4','w'))
    
    # 设置指定组内的键值对
    config.set('section1','k10','123')
    config.write(open('f5','w'))
    

    2. XML

      - traveral

    from xml.etree import ElementTree as ET
    
    # A.利用ElementTree.XML将字符串解析成xml对象
    # str_xml = open('xo.xml','r').read()  # 打开文件,读取XML内同
    # root = ET.XML(str_xml)  # 将字符串解析成xml特殊对象,root代指xml文件的根节点
    # print(root)
    
    
    # B.利用ElementTree.parse将文件直接解析成xml对象
    tree = ET.parse('xo.xml')  # 直接解析xml文件
    root = tree.getroot()  # 获取xml文件的根节点
    # print(root)  # <Element 'data' at 0x0000018AC8B445E8>
    
    # a. 遍历XML文档的所有内容
    # print(root.tag)  # data  顶层标签
    #
    # for child in root:  # 遍历XML文档的第二层
    # 	print(child.tag, child.attrib)  # 第二层节点的标签名称和标签属性和内容
    # 	for i in child:  # 遍历XML文档的第三层
    # 		print('	', i.tag, i.attrib, i.text)  # 第三层节点的标签名称和属性和内容
    
    
    # b.遍历XML中指定的节点
    print(root.tag)
    for node in root.iter('rank'):  # 遍历XML中所有的year节点
    	print(node.tag, node.text)  # 节点的标签名称和内容
    

      - modify

    # 解析字符串方式,修改,保存
    
    # from xml.etree import ElementTree as ET
    # str_xml = open('xo.xml', 'r').read()  # 利用ElementTree.XML将字符串解析成xml对象
    # root = ET.XML(str_xml)  # root为根节点
    # print(root.tag)
    #
    # for node in root.iter('year'):  # 循环所有的year节点
    # 	new_year = int(node.text) + 1  # year节点的内容加1
    # 	node.text = str(new_year)  # year节点内容更新
    #
    # 	node.set('name', 'alex')  # 设置year节点的属性
    # 	node.set('age', '19')  # 设置year节点的属性
    #
    # 	del node.attrib['name']  # 删除year节点的某属性
    #
    # tree = ET.ElementTree(root)  # 创建tree
    # tree.write('xo1.xml', encoding='utf-8')  # 保存文件
    
    
    
    # 解析文件方式,修改,保存
    
    from xml.etree import ElementTree as ET
    tree = ET.parse('xo.xml')  # 直接解析xml文件
    root = tree.getroot()  # 获取xml文件的根节点
    
    for node in root.iter('year'):  # 循环所有的year节点
    	new_year = int(node.text) + 1  # year节点的内容加1
    	node.text = str(new_year)  # year节点内容更新
    
    	node.set('name', 'alex')  # 设置year节点的属性
    	node.set('age', '19')  # 设置year节点的属性
    
    	del node.attrib['name']  # 删除year节点的某属性
    
    tree.write('xo2.xml', encoding='utf-8')  # 保存文件
    

      - delete

    # 解析字符串方式,删除,保存
    
    # from xml.etree import ElementTree as ET
    # str_xml = open('xo.xml', 'r').read()  # 利用ElementTree.XML将字符串解析成xml对象
    # root = ET.XML(str_xml)  # root为根节点
    # print(root.tag)
    #
    # for country in root.findall('country'):
    # 	rank = int(country.find('rank').text)
    # 	if rank > 90:
    # 		root.remove(country)
    #
    # tree = ET.ElementTree(root)  # 创建tree
    # tree.write('xo3.xml', encoding='utf-8')  # 保存文件
    
    
    
    # 解析文件方式,删除节点,保存
    
    # from xml.etree import ElementTree as ET
    # tree = ET.parse('xo.xml')  # 直接解析xml文件
    # root = tree.getroot()  # 获取xml文件的根节点
    #
    # for country in root.findall('country'):
    # 	rank = int(country.find('rank').text)
    # 	if rank > 50:
    # 		root.remove(country)
    #
    # tree.write('xo3.xml', encoding='utf-8')  # 保存文件
    

    3. Pycharm tips

      - 基本设置

        - 不使用tab、tab=4空格:Editor>Code Style>Python
        - 字体、字体颜色:Edit>Colors & Fonts>Python
        - 关闭自动更新:Appearance & Behavior>System Settings>Updates
        - 脚本头设置:Edit>File and Code Templates>Python Script 注:其他类似
        - 显示行号:Edit>General>Appearance>Show line numbers 注:2016.2默认显示行号
        - 右侧竖线是PEP8的代码规范,提示一行不要超过120个字符
        - 导出、导入你自定义的配置: File>Export Settings、Import Settings

      - 常用快捷键,例如复制当前行、删除当前行、批量注释、缩进、查找和替换。

        - 常用快捷键的查询和配置:Keymap
        - Ctrl + D:复制当前行
        - Ctrl + Y:删除当前行
        - Shift + Enter:快速换
        - Ctrl + /:快速注释(选中多行后可以批量注释)
        - Tab:缩进当前行(选中多行后可以批量缩进)
        - Shift + Tab:取消缩进(选中多行后可以批量取消缩进)
        - Ctrl + F:查找
        - Ctrl + R:替换

      - 常用操作指南

        - 复制文件路径:左侧文件列表右键选中的文件>Copy Path
        - 在文件管理器中打开:右键选中的文件>往下找到Show In Explorer
        - 快速定位:Ctrl + 某些内建模块之后,点击在源文件中展开
        - 查看结构:IDE左侧边栏Structure 查看当前项目的结构
        - tab批量换space:Edit>Convert Indents
        - TODO的使用:# TODO 要记录的事情
        - Debug设置断点,直接点击行号与代码之间的空白处即可设置断点(视频里忘了说)
        - Tab页上右键>Move Right(Down),把当前Tab页移到窗口右边(下边),方便对比
        - 文件中右键>Local History能够查看文件修改前后的对比
        - IDE右下角能看到一些有用的信息,光标当前在第几行的第几个字符、当前回车换行、当前编码类型、当前Git分支
        - IDE右侧边栏>Database
        - 单独一行的注释:#+1空格+注释内容          

        - 代码后跟着的注释:2空格+#+1空格+注释内容

  • 相关阅读:
    第一次团队作业——团队展示
    TAC队--团队选题报告
    TAC 坦克队
    Leetcode题库——16.最接近的三数之和
    Leetcode题库——11.盛最多水的容器
    Leetcode题库——23.合并k个排序链表
    Leetcode题库——4.寻找两个有序数组的中位数
    Leetcode题库——15.三数之和
    Leetcode题库——12.整数转罗马数字
    Leetcode题库——19.删除链表的倒数第 n 个节点【##】
  • 原文地址:https://www.cnblogs.com/wnzhong/p/5902063.html
Copyright © 2020-2023  润新知