提交 c20d6f4c 编写于 作者: 秦英杰

fix:python扩展模块

上级 5ad02c3b
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/python-demo.iml" filepath="$PROJECT_DIR$/.idea/python-demo.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module version="4"> <module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings"> <component name="PyDocumentationSettings">
<option name="format" value="PLAIN" /> <option name="format" value="PLAIN" />
<option name="myDocStringFormat" value="Plain" /> <option name="myDocStringFormat" value="Plain" />
......
此差异已折叠。
...@@ -5,24 +5,25 @@ ...@@ -5,24 +5,25 @@
# 打开文件 # 打开文件
import time import time
f = open("/Users/qinyingjie/Downloads/改签合同系统操作指引.pdf", "r", encoding="UTF-8") f = open("/Users/qinyingjie/Documents/python-workspace/python-demo/07_文件操作/05_文件操作的综合案例.py", "r", encoding="UTF-8")
print(type(f)) print(type(f))
# 读取文件 - read() # 读取文件 - read()
# print(f"读取10个字节的结果:{f.read(10)}") print(f"读取10个字节的结果:{f.read(10)}")
# print(f"read方法读取全部内容的结果是:{f.read()}") print("-"*100)
print(f"read方法读取全部内容的结果是:{f.read()}")
print("-----------------------------------------------") print("-----------------------------------------------")
# 读取文件 - readLines() # 读取文件 - readLines()
# lines = f.readlines() # 读取文件的全部行,封装到列表中 lines = f.readlines() # 读取文件的全部行,封装到列表中
# print(f"lines对象的类型:{type(lines)}") # print(f"lines对象的类型:{type(lines)}")
# print(f"lines对象的内容是:{lines}") # print(f"lines对象的内容是:{lines}")
# 读取文件 - readline() # 读取文件 - readline()
# line1 = f.readline() line1 = f.readline()
# line2 = f.readline() line2 = f.readline()
# line3 = f.readline() line3 = f.readline()
# print(f"第一行数据是:{line1}") print(f"第一行数据是:{line1}")
# print(f"第二行数据是:{line2}") print(f"第二行数据是:{line2}")
# print(f"第三行数据是:{line3}") print(f"第三行数据是:{line3}")
# for循环读取文件行 # for循环读取文件行
# for line in f: # for line in f:
...@@ -32,7 +33,7 @@ print("-----------------------------------------------") ...@@ -32,7 +33,7 @@ print("-----------------------------------------------")
# time.sleep(500000) # time.sleep(500000)
# with open 语法操作文件 # with open 语法操作文件
row = 0 row = 0
with open("/Users/qinyingjie/Downloads/资料/第1-12章资料/资料/地图数据/疫情.txt", "r", encoding="UTF-8") as f: with open("/Users/qinyingjie/Documents/python-workspace/python-demo/07_文件操作/05_文件操作的综合案例.py", "r", encoding="UTF-8") as f:
for line in f: for line in f:
row += 1 row += 1
print(f"第{row}行数据是:{line}") print(f"第{row}行数据是:{line}")
......
...@@ -3,19 +3,19 @@ ...@@ -3,19 +3,19 @@
""" """
# 打开文件,以读取模式打开 # 打开文件,以读取模式打开
f = open("D:/word.txt", "r", encoding="UTF-8") f = open("/Users/qinyingjie/Downloads/Snipaste_2024-08-11_22-14-26.png", "r", encoding="UTF-8")
# 方式1:读取全部内容,通过字符串count方法统计itheima单词数量 # 方式1:读取全部内容,通过字符串count方法统计itheima单词数量
# content = f.read() content = f.read()
# count = content.count("itheima") count = content.count("itheima")
# print(f"itheima在文件中出现了:{count}次") # print(f"itheima在文件中出现了:{count}次")
# 方式2:读取内容,一行一行读取 # 方式2:读取内容,一行一行读取
count = 0 # 使用count变量来累计itheima出现的次数 count = 0 # 使用count变量来累计itheima出现的次数
for line in f: for line in f:
line = line.strip() # 去除开头和结尾的空格以及换行符 line = line.strip() # 去除开头和结尾的空格以及换行符
words = line.split(" ") words = line.split(" ")
for word in words: for word in words:
if word == "itheima": if word == "itheima":
count += 1 # 如果单词是itheima,进行数量的累加加1 count += 1 # 如果单词是itheima,进行数量的累加加1
# 判断单词出现次数并累计 # 判断单词出现次数并累计
print(f"itheima出现的次数是:{count}") print(f"itheima出现的次数是:{count}")
# 关闭文件 # 关闭文件
......
...@@ -5,17 +5,18 @@ ...@@ -5,17 +5,18 @@
# 打开文件,不存在的文件, r, w, a # 打开文件,不存在的文件, r, w, a
import time import time
# f = open("D:/test.txt", "w", encoding="UTF-8") f = open("/Users/qinyingjie/Downloads/未命名.txt", "w", encoding="UTF-8")
# # write写入 # # write写入
# f.write("Hello World!!!") # 内容写入到内存中 f.write("Hello World!!!") # 内容写入到内存中
# # flush刷新 # # flush刷新
# # f.flush() # 将内存中积攒的内容,写入到硬盘的文件中 f.flush() # 将内存中积攒的内容,写入到硬盘的文件中
# # close关闭 # close关闭
# f.close() # close方法,内置了flush的功能的 # f.close() # close方法,内置了flush的功能的
print(1111)
# 打开一个存在的文件 # 打开一个存在的文件
f = open("D:/test.txt", "w", encoding="UTF-8") # f = open("D:/test.txt", "w", encoding="UTF-8")
# write写入、flush刷新 # write写入、flush刷新
f.write("黑马程序员") # f.write("黑马程序员")
# close关闭 # close关闭
f.close() # f.close()
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# # close关闭 # # close关闭
# f.close() # f.close()
# 打开一个存在的文件 # 打开一个存在的文件
f = open("D:/test.txt", "a", encoding="UTF-8") f = open("/Users/qinyingjie/Downloads/未命名.txt", "a", encoding="UTF-8")
# write写入、flush刷新 # write写入、flush刷新
f.write("\n月薪过万") f.write("\n月薪过万")
# close关闭 # close关闭
......
...@@ -11,12 +11,14 @@ for line in fr: ...@@ -11,12 +11,14 @@ for line in fr:
line = line.strip() line = line.strip()
# 判断内容,将满足的内容写出 # 判断内容,将满足的内容写出
if line.split(",")[4] == "测试": if line.split(",")[4] == "测试":
continue # continue进入下一次循环,这一次后面的内容就跳过了 # continue进入下一次循环,这一次后面的内容就跳过了
# 将内容写出去 continue
# 将内容写出去
fw.write(line) fw.write(line)
# 由于前面对内容进行了strip()的操作,所以要手动的写出换行符 # 由于前面对内容进行了strip()的操作,所以要手动的写出换行符
fw.write("\n") fw.write("\n")
# close2个文件对象 # close2个文件对象
fr.close() fr.close()
fw.close() # 写出文件调用close()会自动flush() # 写出文件调用close()会自动flush()
fw.close()
...@@ -2,17 +2,22 @@ ...@@ -2,17 +2,22 @@
演示异常的传递性 演示异常的传递性
""" """
# 定义一个出现异常的方法 # 定义一个出现异常的方法
def func1(): def func1():
print("func1 开始执行") print("func1 开始执行")
num = 1 / 0 # 肯定有异常,除以0的异常 num = 1 / 0 # 肯定有异常,除以0的异常
print("func1 结束执行") print("func1 结束执行")
# 定义一个无异常的方法,调用上面的方法 # 定义一个无异常的方法,调用上面的方法
def func2(): def func2():
print("func2 开始执行") print("func2 开始执行")
func1() func1()
print("func2 结束执行") print("func2 结束执行")
# 定义一个方法,调用上面的方法 # 定义一个方法,调用上面的方法
def main(): def main():
...@@ -21,4 +26,5 @@ def main(): ...@@ -21,4 +26,5 @@ def main():
except Exception as e: except Exception as e:
print(f"出现异常了,异常的信息是:{e}") print(f"出现异常了,异常的信息是:{e}")
main() main()
...@@ -22,5 +22,6 @@ ...@@ -22,5 +22,6 @@
# 通过__all__变量,控制import * # 通过__all__变量,控制import *
from my_package import * from my_package import *
my_module1.info_print1() my_module1.info_print1()
my_module2.info_print2() my_module2.info_print2()
...@@ -9,6 +9,5 @@ from my_utils import file_util ...@@ -9,6 +9,5 @@ from my_utils import file_util
print(my_utils.str_util.str_reverse("黑马程序员")) print(my_utils.str_util.str_reverse("黑马程序员"))
print(my_utils.str_util.substr("itheima", 0, 4)) print(my_utils.str_util.substr("itheima", 0, 4))
file_util.append_to_file("D:/test_append.txt", "itheima") file_util.append_to_file("D:/test_append.txt", "itheima")
file_util.print_file_info("D:/test_append.txt") file_util.print_file_info("D:/test_append.txt")
""" """
演示常用的模块功能 演示常用的模块功能-time模块
""" """
import time import time
# time模块 # time模块
ts = time.time() # 当前时间戳 ts = time.time() # 当前时间戳
print(f"当前时间戳是:{ts}") print(f"当前时间戳是:{ts}")
# 获取当前时间以指定的格式显示,2000-01-01 10:00:00 # 获取当前时间以指定的格式显示,2000-01-01 10:00:00
...@@ -11,8 +12,3 @@ print(time.strftime("%Y-%m-%d %H:%M:%S")) ...@@ -11,8 +12,3 @@ print(time.strftime("%Y-%m-%d %H:%M:%S"))
# 将指定的时间戳转换为格式化的日期字符串 # 将指定的时间戳转换为格式化的日期字符串
print(time.strftime("%Y-%m-%d %H:%M:%S")) print(time.strftime("%Y-%m-%d %H:%M:%S"))
# random模块
# os模块
# sys模块
import random
# 生成一个0到1之间的随机浮点数
random_float = random.random()
print(random_float)
import random
# 方式一
# 生成一个随机浮点数,并格式化为两位小数
random_float = random.random()
formatted_float = f"{random_float:.2f}"
print(formatted_float)
# 方式二
# 生成一个随机浮点数,并四舍五入到两位小数
random_float = round(random.random(), 2)
print(random_float)
# 方式三
# 使用format()函数格式化为两位小数
random_float = random.random()
formatted_float = format(random_float, '.2f')
print(formatted_float)
import random
# 从序列中随机选择一个元素
item1 = random.choice(['apple', 'banana', 'cherry'])
print(item1)
# 从序列中随机选择一个元素
item2 = random.sample(['apple', 'banana', 'cherry'], 3)
print(item2)
import random
# 从序列中随机选择多个元素,不重复
items = random.sample(range(1, 11), 5)
print(items)
# 定义一个列表
items = ['apple', 'banana', 'cherry']
# 相同的概率
# 使用random.choices()随机选择3个元素,允许重复
# 第三个参数是每个元素被选择的概率,如果为None,则每个元素被选择的概率相同
# weights=None 表示每个元素被选择的概率相同。
item2 = random.choices(items, weights=None, cum_weights=None, k=3)
print(item2)
# 不同的概率
# 使用 cum_weights 进行选择时,每个元素的选择概率取决于其累积权重与总累积权重的比例。
# 例如,如果总累积权重是 10,而某个元素的累积权重是 5,那么这个元素被选中的概率是 50%。
# 这种方法可以用于实现更复杂的选择逻辑,例如基于概率分布的选择。
weights = [1, 2, 3]
item3 = random.choices(items, weights=weights, cum_weights=None, k=3)
print(item3)
import random
# 定义一个列表
items = ['apple', 'banana', 'cherry']
# 同时指定2个策略会报错
cum_weights = [2, 2, 2]
weights = [1, 2, 3]
item3 = random.choices(items, weights=weights, cum_weights=cum_weights, k=3)
print(item3)
import random
# 定义一个列表
# 不同的累计权重
# 'A' 的累计权重为 1,
# 'B' 的累计权重为 2(1 + 2),
# 'C' 的累计权重为 3(1 + 2 + 3),
# 'D' 的累计权重为 4(1 + 2 + 3 + 4)。
items = ['A', 'B', 'C', 'D']
cum_weights = [1, 3, 6, 10]
item3 = random.choices(items, weights=None, cum_weights=cum_weights, k=3)
print(item3)
import random
# 设置随机数生成器的种子,每次的结果一致,一般用于测试
random.seed(1)
print(random.random())
import os
# 获取当前工作目录
current_directory = os.getcwd()
print(current_directory)
# 列出指定目录下的所有文件和目录名
entries = os.listdir('.')
print(entries)
# 改变当前工作目录
os.chdir('/Users/qinyingjie/Downloads')
# 获取环境变量
path = os.getenv('PATH')
print(path)
import sys
# 打印Python解释器的版本信息
print(sys.version)
# 打印Python解释器的路径
print(sys.executable)
# 获取命令行参数列表
arguments = sys.argv
print(arguments)
# 退出程序
sys.exit(0)
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
演示自定义模块1 演示自定义模块1
""" """
def info_print1(): def info_print1():
print("我是模块1的功能函数代码") print("我是模块1的功能函数代码")
info_print1()
\ No newline at end of file info_print1()
...@@ -18,7 +18,7 @@ def print_file_info(file_name): ...@@ -18,7 +18,7 @@ def print_file_info(file_name):
except Exception as e: except Exception as e:
print(f"程序出现异常了,原因是:{e}") print(f"程序出现异常了,原因是:{e}")
finally: finally:
if f: # 如果变量是None,表示False,如果有任何内容,就是True if f: # 如果变量是None,表示False,如果有任何内容,就是True
f.close() f.close()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册