Python学习教程-3_Use functions

本节的实战例子为利用Python来批量移除照片文件名中含有的数字

1. 在写程序前,首先还是先列出程序大致的流程图:

step 1:获得文件名

step 2:对每一个文件进行重新命名

2. 下载需要重新命名的44张照片,地址为:点击打开链接

3. 在idle中创建 renamefile.py ,然后创建一个同样名字的函数,并将要实现的功能进行注释

def renamefiles():
    #(1) get filenames from a folder
    #(2) for each file,rename filename

renamefiles()
4. 现在开始具体实现,根据  明确需求-google it  原则,google需要的信息(而不是为了一个点去学一个面,这也是入门某种技术时需要避免的)

google:find file names in a folder in python 

我们得到:

os.listdir() will get you everything that's in a directory - files and directories.

接下来利用前面所学:定义os函数:import os  使用listdir函数:listdir()  其中括号内为照片文件夹路径,为了让Python直接按照字符串的方式解读地址,地址“”前需要加r ,然后将此函数的输出存入一个变量中,并打印输出

file_list=os.listdir(r"G:\Programs\Python\prank")

print file_list

5. 接下来进行第二步,通过阅读Python文档,了解os函数中哪个函数可以进行文件的重命名

地址为https://docs.python.org/2/library/os.html

找到rename(src,dst)函数,src参数为原文件名,dst为预期文件名

为了实现对文件名的更改,google到了translate函数,其具体用法及示例如下:

str.translate(table[, deletechars]);
Parameters
table -- You can use the maketrans() helper function in the string module to create a translation table.

deletechars -- The list of characters to be removed from the source string.

Return Value
This method returns a translated copy of the string.

Example
The following example shows the usage of translate() method. Under this every vowel in a string is replaced by its vowel position −

#!/usr/bin/python

from string import maketrans   # Required to call maketrans function.

intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

str = "this is string example....wow!!!";
print str.translate(trantab)
When we run above program, it produces following result −

th3s 3s str3ng 2x1mpl2....w4w!!!
Following is the example to delete 'x' and 'm' characters from the string −

#!/usr/bin/python

from string import maketrans   # Required to call maketrans function.

intab = "aeiou"
outtab = "12345"
trantab = maketrans(intab, outtab)

str = "this is string example....wow!!!";
print str.translate(trantab, 'xm')
This will produce following result −

th3s 3s str3ng 21pl2....w4w!!!

6.完整的代码如下:
import os
def renamefiles():
    #(1) get filenames from a folder
    file_list=os.listdir(r"G:\Programs\Python\prank")
    #print (file_list)
    saved_path = os.getcwd()
    print("Current working directory is"+saved_path)
    os.chdir(r"G:\Programs\Python\prank")
    #(2) for each file,rename filename
    for file_name in file_list:
        os.rename(file_name,file_name.translate(None,"0123456789"))
        print("old name: "+file_name)
        print("new name: "+file_name.translate(None,"0123456789"))

renamefiles()

7.注:其中oslistdir是为了显示当前工作目录,os.chdir函数是为了将程序的工作目录改为想要操作的目录,两个Print是为了输出原文件名和修改后的文件名。

8.注意还使用了for循环来进行循环更改。

9.入门一门技术,想要一次学会所有是不可能的,并且也不可能记住,因此使用时实时检索,现学现用的能力十分重要。因此,查看官方文档并加以适当的练习是快速入门的捷径。

10. 推荐网站:https://docs.python.org/2/index.html

  以及很好的在线学习教程网站:https://www.tutorialspoint.com/index.htm


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Python来实现Ziegler-Nichols方法进行自动调节PID值的步骤如下: 1. 设定PID控制器的积分和微分部分为零,只保留比例部分。 2. 增大比例参数,观察系统的响应。 3. 根据响应曲线的形状确定临界增益(即系统开始震荡的增益值)。 4. 根据临界增益计算出比例、积分和微分参数的初值。 这里提供一个简单的示例代码,你可以根据你的具体系统进行修改: ```python import time import numpy as np import matplotlib.pyplot as plt # Simulate the system response def simulate_system(Kp, Ki, Kd): # Simulate your system here # Return the system response pass # Ziegler-Nichols method for tuning PID parameters def ziegler_nichols_tuning(): # Initial PID parameters Kp = 0.0 Ki = 0.0 Kd = 0.0 # Initial step size for increasing Kp step_size = 0.1 # Settling time threshold for oscillations to stabilize settling_time_threshold = 2.0 # Start with increasing Kp until oscillations occur while True: Kp += step_size # Simulate the system response response = simulate_system(Kp, Ki, Kd) # Measure the settling time of the response settling_time = calculate_settling_time(response) # If oscillations occur, stop increasing Kp if settling_time < settling_time_threshold: break # Calculate the critical gain Kc Kc = Kp # Calculate the ultimate period Tu (time period of oscillations) Tu = calculate_ultimate_period(response) # Calculate PID parameters based on Ziegler-Nichols formulas Kp = 0.6 * Kc Ki = 1.2 * Kc / Tu Kd = 0.075 * Kc * Tu return Kp, Ki, Kd # Helper functions for calculating settling time and ultimate period def calculate_settling_time(response): # Calculate the settling time of the response # You can use your own criteria to determine the settling time pass def calculate_ultimate_period(response): # Calculate the ultimate period (Tu) of the response # You can use your own method to estimate the ultimate period pass # Main function def main(): # Tune PID parameters using Ziegler-Nichols method Kp, Ki, Kd = ziegler_nichols_tuning() # Print the tuned PID parameters print("Tuned PID parameters:") print("Kp =", Kp) print("Ki =", Ki) print("Kd =", Kd) if __name__ == '__main__': main() ``` 以上代码是一个简单的示例,你需要根据你的实际系统进行适当的修改,包括编写`simulate_system`函数来模拟系统响应,并实现`calculate_settling_time`和`calculate_ultimate_period`函数来计算响应的稳定时间和最终周期。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值