Python学习记录-xml转换

写代码过程记录

os.getcwd() # 获取当前目录
substr in str: # 判断子串是否在串中
str.find(substr) == -1: # 同上

获取目录下的所有文件

def file_names(root_dir):
    for root, dirs, files in os.walk(root_dir):
        print(root) # 当前目录路径
        print(dirs) # 当前路径下所有子目录
        print(files) # 当前路径下所有非目录子文件
def listdir(path, list_name):  #传入存储的list
    for file in os.listdir(path):  
        file_path = os.path.join(path, file)  
            if os.path.isdir(file_path):  
                listdir(file_path, list_name)  
            else:  
                list_name.append(file_path)  

xml解析

from xml.etree import ElementTree as et

# 直接从硬盘文件解析
tree = et.parse('file.xml')
# 根节点
root = tree.getroot()
# 每个节点都包含以下属性
"""
1. tag:string对象,表示数据代表的种类。
2. attrib:dictionary对象,表示附有的属性。
3. text:string对象,表示element的内容。
4. tail:string对象,表示element闭合之后的尾迹。
5. 若干子元素(child elements)。
"""

# 修改某值
tree.write('new_file.xml')

python interface

from abc import ABCMeta
from abc import abstractmethod

class Alert():
    __metaclass__ = ABCMeta
    
    @abstractmethod
    def send(self):
        pass

class Weixin(Alert):
    def __init__(self):
        print("初始化")
    
    # 必须定义的抽象方法
    def send(self):
        print("发送")
w = Weixin()
W.send()

python callback

def callback(input):
    print(input)
    print("this is a callback")

def caller(input, func):
    func(input)

caller(1, callback)

python opencv 安装

sudo apt-get install python-opencv
# 另外一种尝试
brew install opencv
pip install opencv-python
pip install opencv-contrib-python

猜你喜欢

转载自blog.csdn.net/weixin_33742618/article/details/87048581