modify scripts
This commit is contained in:
64
aabook/tools/tools_diff.py
Normal file
64
aabook/tools/tools_diff.py
Normal file
@ -0,0 +1,64 @@
|
||||
import sys
|
||||
import csv
|
||||
|
||||
def compare_files(file_a, file_b):
|
||||
"""
|
||||
比较两个文件,并输出差异
|
||||
|
||||
Args:
|
||||
file_a (str): 文件 A 的路径
|
||||
file_b (str): 文件 B 的路径
|
||||
"""
|
||||
|
||||
try:
|
||||
# 创建输出文件
|
||||
with open('need_update.txt', 'w', newline='') as f_update, \
|
||||
open('old_only.txt', 'w', newline='') as f_b_only:
|
||||
writer_update = csv.writer(f_update, delimiter='\t')
|
||||
writer_b_only = csv.writer(f_b_only, delimiter='\t')
|
||||
|
||||
# 读取文件 A
|
||||
data_a = {} # 使用字典存储,key为开始链接编码,value为整行数据
|
||||
with open(file_a, 'r') as f:
|
||||
reader = csv.reader(f, delimiter='\t')
|
||||
for row in reader:
|
||||
data_a[row[6]] = row
|
||||
|
||||
# 读取文件 B
|
||||
data_b = {}
|
||||
with open(file_b, 'r') as f:
|
||||
reader = csv.reader(f, delimiter='\t')
|
||||
for row in reader:
|
||||
data_b[row[6]] = row
|
||||
|
||||
# 比较并输出
|
||||
for key, value in data_a.items():
|
||||
if key not in data_b:
|
||||
writer_update.writerow(value)
|
||||
else:
|
||||
if abs(int(value[8]) - int(data_b[key][8])) > 100:
|
||||
writer_update.writerow(value)
|
||||
|
||||
for key, value in data_b.items():
|
||||
if key not in data_a:
|
||||
writer_b_only.writerow(value)
|
||||
|
||||
except FileNotFoundError:
|
||||
print(f"文件不存在: {file_a} 或 {file_b}")
|
||||
except csv.Error as e:
|
||||
print(f"CSV文件读取错误: {e}")
|
||||
except Exception as e:
|
||||
print(f"发生未知错误: {e}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) != 3:
|
||||
print("Usage: python script.py file_a file_b")
|
||||
print("file_a : 新下载的列表,通常按照更新时间排序")
|
||||
print("file_b : 以前下载的列表")
|
||||
print("输出 need_update.txt : 需要更新的列表")
|
||||
print("输出 old_only.txt : 仅在较早下载列表中的行,通常不应该有")
|
||||
sys.exit(1)
|
||||
|
||||
file_a = sys.argv[1]
|
||||
file_b = sys.argv[2]
|
||||
compare_files(file_a, file_b)
|
||||
Reference in New Issue
Block a user