You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
main/calculate.py

64 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 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)}"