课程大纲
第二章 Python10分钟入门
【2.1】:PyCharm社区版配置Anaconda开发环境
【2.2】:Python基础知识及正则表达式入门
第三章 Python操作Excel
【3.1】:xlrd 使用教程 读取 操作Excel
【3.2】:xlwt 使用教程 写入 操作Excel
【3.3】:xlutils 使用教程 修改 操作Excel
【3.4】:xlwings 使用教程 读取 写入 修改 操【作Excel
【3.5】:openpyxl 使用教程 读取 写入 修改 操作Excel
【3.6】:xlswriter 使用教程 读取 写入 修改 操作Excel
【3.7】:win32com 使用教程 读取 写入 修改 操作Excel
【3.8】:pandas 使用教程 读取 写入 修改 操作Excel
第四章 Python操作word
【4.1】:win32com 使用教程 操作word
【4.2】:python-docx 使用教程 操作word
第五章 Python操作ppt
【5.1】:win32com 使用教程 操作复制ppt PowerPoint
【5.2】:python-pptx 使用教程 操作ppt PowerPoint 添加文字 形状图表
文章目录
本章讲述Python最常用的基础知识,最后一小节讲述了如何在Python中使用正则表达式来匹配字符串。
2.2 Python 10分钟快速入门
2.2.1 输入、输出和注释
# 我是单行注释,我并不会执行
print("Hello Python!")
name = input()
print('hello,', name)
"""
我是多行注释,我也不会被执行
"""
2.2.2 调用本文件的函数
main.py:
# 创建函数
def myFirstPythonFun():
print("我的第一个Python函数")
if __name__ == '__main__':
# 我是注释,我并不会执行
print("程序从这里开始运行")
# 调用函数
myFirstPythonFun()
2.2.3 调用本文件路径下其它文件的函数
① 新建 module1.py,键入以下代码:
module1.py:
def otherFileFun():
"""
我是不同文件的一个函数
:return: 一个字符串,可以为任意类型
"""
print("我 module1 里面的函数,我被调用了!")
return "module文件内 otherFileFun函数的 返回值"
② 在 main.py 中调用module函数
main.py:
import module1
import subdirectory.subdirectoryFile as subDir
# 创建函数
def myFirstPythonFun():
print("我的第一个Python函数\n")
if __name__ == '__main__':
# 我是注释,我并不会执行
print("程序从这里开始运行\n")
# 调用本文件函数
myFirstPythonFun()
# 调用其他文件函数
print("在此调用其他文件函数\n---------")
result = module1.otherFileFun()
print("函数返回值为:\n---------")
print(result)
2.2.4 调用子目录下其它文件内的函数
① 创建子目录,并建立python文件
subdiretoryFile.py:
# 我是子文件目录的函数
def subDirFun():
print("子目录下的函数被调用")
return 0
② 子目录下函数调用
main.py:
import module1
import subdirectory.subdirectoryFile as subDir
# 创建函数
def myFirstPythonFun():
print("我的第一个Python函数\n")
if __name__ == '__main__':
# 我是注释,我并不会执行
print("程序从这里开始运行\n")
# 调用本文件函数
myFirstPythonFun()
# 调用其他文件函数
print("在此调用其他文件函数\n---------")
result = module1.otherFileFun()
print("函数返回值为:\n---------")
print(result)
# 调用子目录文件的函数
subResult = subDir.subDirFun()
print("子目录函数返回结果:",subResult)
2.2.5 数据类型和变量
推荐阅读:https://www.liaoxuefeng.com/wiki/1016959663602400/1017063413904832
Python的语法比较简单,采用缩进方式,写出来的代码就像下面的样子:
# print absolute value of an integer:
a = 100
if a >= 0:
print(a)
else:
print(-a)
数据类型测试函数:
# 数据类型测试函数
def dataTypeTest():
# 1.数字 整型、浮点型、布尔型
dt_int = 100
dt_float = 99.99
dt_bool = True
print("dt_int value = ",dt_int,"typeof(dt_int) = ",type(dt_int))
print("dt_float value = ",dt_float,"typeof(dt_float) = ", type(dt_float))
print("dt_bool value = ", dt_bool, "typeof(dt_bool) = ", type(dt_bool),"\n")
# 2.字符串 单行、多行
dt_singlestring = "单行字符串"
dt_multistring = """---
多行
字符串
"""
print(dt_singlestring)
print(dt_multistring)
# 3.列表 可以存放数字,字符串等
dt_list1 = [1,2,3,4,5]
dt_list2 = ['你好','thank you', 222]
print(dt_list1)
print(dt_list2,"\n")
# 4.元组 生成之后不能修改
dt_tuple = (1,2,3,4,5)
print(dt_tuple,"\n")
# 5.字典 可以存放键值对
dt_dict = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
print(dt_dict)
print(dt_dict['Michael'],"\n")
# 6.集合 不能有重复的值
dt_set = set([1, 1, 2, 2, 3, 3])
print(dt_set)
输出结果:
2.2.6 条件判断
条件判断测试函数:
# 条件判断测试函数
def if_else_test():
age = int(input('Input your age: '))
if age >= 18:
print('adult')
elif age >= 6:
print('teenager')
else:
print('kid')
输出结果:
2.2.7 循环
① for循环
for测试函数
def circlefor_test():
# 打印list:
names = ['Michael', 'Bob', 'Tracy']
for name in names:
print(name)
# 打印数字 0 - 9
for x in range(10):
print(x)
② while循环
while测试函数:
def circlewhile_test():
# 计算1+2+3+...+100:
sum = 0
n = 1
while n <= 100:
sum = sum + n
n = n + 1
print(sum)
# 计算1x2x3x...x10:
acc = 1
n = 1
while n <= 10:
acc = acc * n
n = n + 1
print(acc)
2.2.8 正则表达式
① 介绍
字符串是编程时涉及到的最多的一种数据结构,对字符串进行操作的需求几乎无处不在。
正则表达式是一种用来匹配字符串的强有力的武器。它的设计思想是用一种描述性的语言来给字符串定义一个规则,凡是符合规则的字符串,我们就认为它“匹配”了,否则,该字符串就是不合法的。
② 工具下载
正则表达式测试器:https://www.cr173.com/soft/88309.html
③ 正则表达式教程
此处,作为例子,我们就在一串字符串里面分别找出 问题描述、原因分析、整改建议和整改方案。
字符串内容为:
[问题描述]:
1.我是问题描述的内容1
1.我是问题描述的内容2
[原因分析]:
我是原因分析的内容。。。
[整改建议]:
我是整改建议的内容。。。
[整改方案]:
我是整改方案的内容。。。
使用以下正则表达式可匹配到 “问题描述” 的相应内容:
\[问题描述\]:([\s\S]*)\[原因分析\]
使用工具来做测试:
④ 在python中使用正则表达式
import re
正则表达式测试函数:
# 正则表达式测试函数
def regex_test():
source_string = """[问题描述]:
1.我是问题描述的内容1
1.我是问题描述的内容2
[原因分析]:
我是原因分析的内容。。。
[整改建议]:
我是整改建议的内容。。。
[整改方案]:
我是整改方案的内容。。。
"""
print(source_string)
regex_str = "\[问题描述\]:([\s\S]*)\[原因分析\]"
match_result = re.match(regex_str, source_string)
if(match_result != None):
print(match_result)
result_str = match_result[1]
print("匹配到的结果为:\n",result_str)
输出结果: