feat: add hash log
This commit is contained in:
@@ -54,21 +54,35 @@ def write_to_csv(data, project):
|
||||
"""
|
||||
csv_file_path = get_csv_file_path(project)
|
||||
file_exists = os.path.isfile(csv_file_path)
|
||||
with open(csv_file_path, mode="a", newline="", encoding="utf-8") as csvfile:
|
||||
fieldnames = [
|
||||
"time",
|
||||
"id",
|
||||
"name",
|
||||
"score",
|
||||
"running_time",
|
||||
"details",
|
||||
"project",
|
||||
]
|
||||
|
||||
fieldnames = ["time", "id", "name", "score", "running_time", "details", "project"]
|
||||
|
||||
existing_data = []
|
||||
|
||||
# 文件存在,先保存
|
||||
if file_exists:
|
||||
with open(csv_file_path, mode="r", newline="", encoding="utf-8") as csvfile:
|
||||
reader = csv.DictReader(csvfile)
|
||||
fieldnames = reader.fieldnames # 获取原表头
|
||||
existing_data = list(reader) # 读数据
|
||||
|
||||
# 扩展表头
|
||||
for field in data.keys():
|
||||
if field not in fieldnames:
|
||||
fieldnames.append(field)
|
||||
|
||||
# 重写
|
||||
with open(csv_file_path, mode="w", newline="", encoding="utf-8") as csvfile:
|
||||
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
||||
|
||||
if not file_exists:
|
||||
writer.writeheader()
|
||||
# 写表头
|
||||
writer.writeheader()
|
||||
|
||||
# 写回数据
|
||||
if existing_data:
|
||||
writer.writerows(existing_data)
|
||||
|
||||
# 写新数据
|
||||
writer.writerow(data)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user