From 49839bce032dc61437cf37309a6aa08c53cc23b3 Mon Sep 17 00:00:00 2001 From: p2apfqmgs <1186275832@qq.com> Date: Sun, 27 Apr 2025 08:11:24 +0800 Subject: [PATCH] Update calculate.py --- calculate.py | 101 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 63 insertions(+), 38 deletions(-) diff --git a/calculate.py b/calculate.py index a401a2b..c74d11d 100644 --- a/calculate.py +++ b/calculate.py @@ -1,39 +1,64 @@ -# calculate.py 文件 -import time # 导入时间模块,用于控制计算频率 [3](@ref) -import csv - - - -def main(a, w1): - try: - x = 1 - while True: - y = int(a)+x # 将输入字符串转换为整数并计算 [3](@ref) - w1.insert(1.0, str(y)+'\n') # 在文本区域插入计算结果 [3](@ref) - time.sleep(1) # 暂停1秒,控制刷新频率 [3](@ref) - x += 1 - except Exception: - w1.insert(1.0, '请输入数字\n') # 捕获异常并提示用户 [3](@ref) - - - - -def query_isbn(isbn, csv_path='datas/books.csv'): - """ - 同步查询ISBN信息(直接返回结果) - - :param isbn: ISBN编号(字符串) - :param csv_path: CSV文件路径(默认当前目录) - :return: 匹配记录字典或错误信息字符串 - """ - try: - with open(csv_path, 'r', encoding='utf-8') as f: - reader = csv.DictReader(f) - for row in reader: - if row['isbn'].strip() == isbn.strip(): # 增加空格过滤 - return row - return "未找到匹配的图书记录" - except FileNotFoundError: - return f"错误:文件 {csv_path} 不存在" - except Exception as e: +# calculate.py 文件 +import time # 导入时间模块,用于控制计算频率 [3](@ref) +import csv + + + +def main(a, w1): + try: + x = 1 + while True: + y = int(a)+x # 将输入字符串转换为整数并计算 [3](@ref) + w1.insert(1.0, str(y)+'\n') # 在文本区域插入计算结果 [3](@ref) + time.sleep(1) # 暂停1秒,控制刷新频率 [3](@ref) + x += 1 + except Exception: + w1.insert(1.0, '请输入数字\n') # 捕获异常并提示用户 [3](@ref) + + + + +def query_isbn(isbn, csv_path='datas/books.csv'): + """ + 同步查询ISBN信息(直接返回结果) + :param isbn: ISBN编号(字符串) + :param csv_path: CSV文件路径 + :return: 匹配记录列表或错误信息字符串 + """ + try: + with open(csv_path, 'r', encoding='utf-8') as f: + reader = csv.DictReader(f) + results = [] + for row in reader: + if row['isbn'].strip() == isbn.strip(): + results.append(row) + return results if results else ["未找到匹配的图书记录"] + except FileNotFoundError: + return [f"错误:文件 {csv_path} 不存在"] + except Exception as e: + return [f"查询失败:{str(e)}"] + + + +def fuzzy_search_book(keyword, csv_path='datas/books.csv'): + """ + 模糊搜索图书信息 + :param keyword: 搜索关键词 + :param csv_path: CSV文件路径 + :return: 匹配记录列表或错误信息字符串 + """ + try: + with open(csv_path, 'r', encoding='utf-8') as f: + reader = csv.DictReader(f) + results = [] + for row in reader: + # 在书名、作者和分类中搜索关键词 + if (keyword.lower() in row['title'].lower() or + keyword.lower() in row['author'].lower() or + keyword.lower() in row['category'].lower()): + results.append(row) + return results if results else "未找到匹配的图书记录" + except FileNotFoundError: + return f"错误:文件 {csv_path} 不存在" + except Exception as e: return f"查询失败:{str(e)}" \ No newline at end of file