【飞桨/百度领航团/零基础Python】百度飞桨领航团零基础Python速成营笔记-06

课程链接:课程传送门

一、文件处理

1.1、文件打开

  • #建议使用这种方法,避免忘记close
    with open('work/train_data_cor.txt') as f:
        for line in f:
            data = line.strip().split(',')
            print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+str(data))
    

1.1.2、文件内数据有问题

1.1.2.1、使用异常跳过有问题的数据
  • f = open('work/train_data_wrg.txt')
    for line in f:
        data = line.strip().split(',')
        try:
            print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+str(data))
        except:
            pass
    f.close()
    
1.1.2.2、增加代码判断
  • f = open('work/train_data_wrg.txt')#1
    for line in f:#2
        data = line.strip().split(',')
        if len(data) != 1:
            print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+str(data))
    
    f.close()
    
1.1.2.3、若文件不存在
  • f = open('work/train_data1.txt') for line in f:
    data = line.strip().split(',')
    if len(data) != 1:
        print('姓名:'+data.pop(0)+'生日:'+data.pop(0)+'时间:'+data)
    f.close()
    

1.2、file对象的函数列表

  • with open('work/train_data.txt') as f:
        data = f.read()
        print('整个文件\n'+data)
        f.seek(0)
        data = f.read(10)
        print('读取指定大小的文件内容\n'+data)
        print(f.tell())
    '''
    整个文件
    james,2004-5-21,2.34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22
    julie,2006-5-9,2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21
    kenny
    sarah,2004-3-8,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55
    mikey,2003-9-10,2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38
    
    读取指定大小的文件内容
    james,2004
    10
    '''
    

1.3、文件的写入

  • f = open('work/data.txt','w')
    f.write('this is file content')
    f.close()
    

二、json

1.1、对象转json

  • import json
    class Athlete(json.JSONEncoder):   #继承json.JSONEncoder
        def __init__(self,a_name,a_dob=None,a_times=[]):
            self.name = a_name
            self.dob = a_dob
            self.times = a_times
        def top3(self):
            return sorted(set([self.sanitize(t) for t in self.times]))[0:3]
        def sanitize(self,time_string):
            if '-' in time_string:
                splitter = '-'
            elif ':' in time_string:
                splitter = ':'
            else:
                return (time_string)
            (mins,secs) = time_string.split(splitter)
            return (mins+'.'+secs)
    
    
    with open('work/train_data_cor.txt') as f:
        data = f.readline().strip().split(',')
        ath = Athlete(data.pop(0),data.pop(0),data)
        print(ath)
    
    ath_json = json.dumps(ath.__dict__)   #.__dict__把ath转换成字典的形式
    print(ath_json)
    

1.2、类中的json形式的变量保存到文件

  • with open('work/json.txt','w') as f:
        json.dump(ath_json,f)  #注意dump与dumps的区别
    '''
    "{\"name\": \"james\", \"dob\": \"2004-5-21\", \"times\": [\"2.34\", \"3:21\", \"2.34\", \"2.45\", \"3.01\", \"2:01\", \"2:01\", \"3:10\", \"2-22\"]}"
    '''
    

1.3、读取json文件内容

  • with open('work/json.txt') as f:
        ath = json.load(f)
        print(ath)
    '''
    {"name": "james", "dob": "2004-5-21", "times": ["2.34", "3:21", "2.34", "2.45", "3.01", "2:01", "2:01", "3:10", "2-22"]}
    '''
    

三、目录访问

3.1、目录访问常见命令

3.1.1、

  • import os
    #返回当前工作目录
    current_path = os.getcwd()
    print('当前路径:'+current_path)
    

3.1.2

  • #改变当前工作目录
    os.chdir('/home/aistudio/work')
    #运行mkdir命令
    os.system('mkdir today')
    

3.1.3

  • from pathlib import Path
    #返回当前绝对路径
    abs_path = os.path.abspath('')
    print('abs_path:'+abs_path)
    #路径是否存在
    Path(abs_path).exists()
    

3.1.4

  • print('当前路径:'+os.getcwd())
    listdir = os.listdir()
    #返回当前路径下文件和文件夹名
    print(listdir)
    

3.1.5

  • #是否为文件夹
    os.path.isdir('/home/aistudio/work/today')
    

3.2、显示work路径下的所有类型为txt的文件

  • import os
    listdir = os.listdir('/home/aistudio/work')
    
    target = []
    for name in listdir:
        #防止文件名与文件夹名一样的情况
        # print(os.path.isfile(name))
        
        temp = name.split('.')
        (filename,filetype) = (temp.pop(0),temp.pop(0))
        if filetype == 'txt':
            target.append(name)
     
        # print('name:%s,type:%s' %(filename,filetype))
    
    print(target)
    
  • import pdb:debug时引入

3.3、显示文件夹中的txt文件

  • import os
    
    target = []
    
    path = '/home/aistudio/work'
    listdir = os.listdir(path)
    for name in listdir:
        #防止文件名与文件夹名一样的情况
        if os.path.isfile(path+'/'+name):
            temp = name.split('.')
            (filename,filetype) = (temp.pop(0),temp.pop(0))
            if filetype == 'txt':
                target.append(name)  
        else:
            #如果是文件夹,需要读取该文件夹的列表        
            dir_path = path+'/'+name
            listdir = os.listdir(dir_path)
            for name in listdir:
                #防止文件名与文件夹名一样的情况
                if os.path.isfile(dir_path+'/'+name):
                    temp = name.split('.')
                    (filename,filetype) = (temp.pop(0),temp.pop(0))
                    if filetype == 'txt':
                        target.append(name)
    print('结果:'+str(target))
    

3.4、递归遍历文件夹内所有的txt文件

  • import os
    
    def recur(path):
        listdir = os.listdir(path)
        for name in listdir:
            if name[0] is '.' or name[0] is '_':
                continue
            next_path = path+'/'+name
            if os.path.isfile(next_path) :
                # print(next_path + '=====isfile')
                temp = name.split('.')
                (filename,filetype) = (temp.pop(0),temp.pop(0))
                if filetype == 'txt':
                    target.append(name)
            else:
                recur(next_path)
        return os.path.dirname(next_path)
    path = '/home/aistudio/work'
    target = []
    recur(path)
    print(target)
    

四、进程和线程

  • import threading, zipfile
    
    class AsyncZip(threading.Thread):
        def __init__(self, infile, outfile):
            threading.Thread.__init__(self)
            self.infile = infile
            self.outfile = outfile
        def run(self):
            f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
            f.write(self.infile)
            f.close()
            print('压缩完成,您要的文件在:', self.outfile)
    
    background = AsyncZip('work/loren.txt', 'work/myarchive.zip')
    print('压缩作业开始了,请您耐心等待...')
    background.start()
    print('我正在为您压缩,请问还需要帮您做什么呢?')
    background.join()
    
  • image-20210209213314707
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是三个无人车通过领航-跟随编队的基于滑模控制器的Python代码: ```python import numpy as np import math # 定义滑模控制器的参数 k1 = 1.0 k2 = 1.0 lambda1 = 1.0 lambda2 = 1.0 # 定义领航车的目标速度 v_des = 10.0 # 定义无人车的初始位置和速度 x1 = np.array([0.0, 0.0]) v1 = np.array([0.0, 0.0]) x2 = np.array([5.0, 0.0]) v2 = np.array([0.0, 0.0]) x3 = np.array([10.0, 0.0]) v3 = np.array([0.0, 0.0]) # 定义无人车的控制量 u1 = np.array([0.0, 0.0]) u2 = np.array([0.0, 0.0]) u3 = np.array([0.0, 0.0]) # 定义时间步长 dt = 0.1 # 定义仿真时间 t_end = 100.0 # 定义仿真步数 n_steps = int(t_end / dt) # 定义跟随误差 e1 = np.array([0.0, 0.0]) e2 = np.array([0.0, 0.0]) e3 = np.array([0.0, 0.0]) # 定义领航车的轨迹 x_des = np.zeros((n_steps, 2)) y_des = np.zeros((n_steps, 2)) for i in range(n_steps): x_des[i] = np.array([i * v_des * dt, 0.0]) y_des[i] = np.array([i * v_des * dt, 5.0]) # 开始仿真 for i in range(n_steps): # 计算跟随误差 e1 = x1 - x_des[i] e2 = x2 - y_des[i] e3 = x3 - y_des[i] # 计算滑模控制器的控制量 u1 = -k1 * e1 - k2 * np.tanh(lambda1 * np.linalg.norm(e1)) * e1 / np.linalg.norm(e1) u2 = -k1 * e2 - k2 * np.tanh(lambda2 * np.linalg.norm(e2)) * e2 / np.linalg.norm(e2) u3 = -k1 * e3 - k2 * np.tanh(lambda2 * np.linalg.norm(e3)) * e3 / np.linalg.norm(e3) # 更新速度和位置 v1 = v1 + u1 * dt v2 = v2 + u2 * dt v3 = v3 + u3 * dt x1 = x1 + v1 * dt x2 = x2 + v2 * dt x3 = x3 + v3 * dt ``` 在这段代码中,我们首先定义了滑模控制器的参数,包括$k_1$,$k_2$,$\lambda_1$和$\lambda_2$。然后,我们定义了领航车的目标速度$v_{des}$和三个无人车的初始位置和速度。接下来,我们定义了无人车的控制量$u_1$,$u_2$和$u_3$,以及时间步长$dt$和仿真时间$t_{end}$。然后,我们计算了领航车的轨迹$x_{des}$和$y_{des}$。最后,我们开始了仿真,计算了跟随误差$e_1$,$e_2$和$e_3$,以及滑模控制器的控制量$u_1$,$u_2$和$u_3$,并更新了无人车的速度和位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值