在文件中寻找特定的元素并保存(2)
需求此篇是在文件中寻找特定的元素并保存(1)的需求迭代。
1 遍历文件中的内容,将带有 xref:xxx.adoc[] 的行内容全部找到;
2 对行内容做二次处理,只保留 xref:xxx.adoc[] 这个字段。
代码12345678910file_input = r"xxx"file_output = r"xxx/list.txt"with open(file_input ,"r",encoding="utf-8") as file1, open(file_output,"w",encoding="utf-8") as file2: for line in file1: if "xref:" in line: index_start = line.find("xref:") index_end = line.find(".adoc") ...
在文件中寻找特定的元素并保存(1)
需求遍历文件中的内容,将带有 “id=” 的行内容全部找到写到一个txt文件中。
代码123456789file_input = r"xxx"file_output = r"xxx/list.txt"#边读边写with open(file_input ,"r",encoding="utf-8") as file1, open(file_output,"w",encoding="utf-8") as file2: for line in file1: if "id=" in line: file2.write(line)
数组对比
需求对比两个列表,找到两个列表中不一样的部分。
其中一个列表是固定的,另一个列表需要每次遍历文件和文件夹。
代码1234567891011121314151617import os# 获取当前工作目录下的文件和文件夹列表list_1 = os.listdir(os.getcwd())# 预定义要比较的文件和文件夹列表list_2 = [".vscode", "_static", "_templates", "conf.py", "make_file_docx.py", "make_file_html.py", "test.py"]# 使用对称差异运算符(^)来找出两个列表之间的不同项different_folder_list = list(set(list_1) ^ set(list_2)if len(different_folder_list) != 0: for different_folder in different_folder_l ...
批量修改文件名
需求修改某文件夹中的所有.png文件的文件名。
要求都加一个前缀 blog- ,比如 test.png 改为 blog-test.png 。
代码123456789101112131415import os# 输入文件夹路径path = input("输入文件夹路径: ") prefix = "blog_"# 遍历指定路径下的文件和子文件夹for root, dirs, files in os.walk(path): for file in files: if file.endswith(".png"): # 检查文件名是否以'.png'结尾 old_name = os.path.join(root, file) new_name = os.path.join(root, prefix + file) os.rename(old_name, new_name) # 重命名文件 print(old ...
一个选择文件并显示路径的GUI
需求使用 tkinter 创建一个GUI,GUI 分为两部分,其中一个按钮点击后可选择文件,另一个文本显示窗口将显示文件路径。
代码1234567891011121314151617181920212223242526import tkinter as tkfrom tkinter import filedialog# 定义按钮调用的函数,即文件选择def open_file_dialog(): file_path = filedialog.askopenfilename() # 使用askopenfilename将返回值赋值给file_path if file_pate: text_var.set(file_path) # 标签显示文件名# 创建主窗口root = tk.Tk()root.title("File Selection GUI")# 创建按钮,点击按钮调用 open_file_dialog 函数button = tk.Button(root, text="选择文件", command=open_file_dialo ...
tkinter.filedialog
简介tkinter.filedialog 模块提供了与文件和文件夹对话框有关的函数,用于在 GUI 应用程序中让用户选择文件和文件夹。以下是一些常用的 tkinter.filedialog 函数:让我们更详细地了解 tkinter.filedialog 模块中的每个主要函数:
1. askopenfilename123from tkinter import filedialogfile_path = filedialog.askopenfilename(title="Select a file", filetypes=[("Text files", "*.txt"), ("All files", "*.*")])
title: 对话框标题。
filetypes: 可选文件类型,以元组列表的形式提供。每个元组包含两个值:文件类型的描述和对应的文件扩展名。用户只能选择指定类型的文件。
返回值: 所选文件的路径。
2. askopenfilenames123from tkinter ...
在图片的不同区域增加不同的超链接
代码123456789101112131415161718192021<html><head> <!-- 设置页面标题 --> <title>点击图片的左边是百度,右边是微博</title></head><body> <!-- 插入图片,并应用图像映射 --> <img src="./icon-hexo.png" usemap="#top_bg"/> <!-- 定义图像映射 --> <map name="top_bg"> <!-- 定义百度链接的矩形区域 --> <area alt="百度" href="http://www.baidu.com" shape="rect" target="blank" coords="0,0,3 ...
openpyxl
简介openpyxl 是一个用于读写 Excel 文件(xlsx 格式)的 Python 库。它提供了丰富的功能,使你能够创建、修改和读取 Excel 文档。以下是关于 openpyxl 的详细介绍:
1pip install openpyxl
基本用法创建一个新的 Excel 文档:1234567891011121314import openpyxl# 创建一个新的 Excel 文档workbook = openpyxl.Workbook()# 获取默认的工作表sheet = workbook.active# 写入数据sheet['A1'] = 'Hello'sheet['B1'] = 'World'# 保存文件workbook.save('example.xlsx')
读取现有的 Excel 文档:12345678910111213import openpyxl# 加载现有的 Excel 文档workbook = openpyxl.load_workbook('exam ...
pyautogui
简介pyautogui 是一个用于自动化鼠标和键盘操作的 Python 库。它可以用于创建自动化脚本,执行重复性任务,模拟用户输入等。
函数及其说明
功能分类
功能描述和示例
鼠标操作
获取鼠标位置
x, y = pyautogui.position()
移动鼠标到指定位置
pyautogui.moveTo(x, y, duration=1)
移动鼠标相对距离
pyautogui.move(100, 50, duration=1)
点击鼠标左键
pyautogui.click(x, y)
点击鼠标右键
pyautogui.rightClick(x, y)
点击鼠标中键
pyautogui.middleClick(x, y)
拖拽鼠标
pyautogui.dragTo(x, y, duration=1)
键盘操作
发送按键
pyautogui.press('enter')
发送组合键
pyautogui.hotkey('ctrl', 'c')
输入文本
pyautogui.typew ...