def update_contract(file_path, temp_file_path, contract_name, increase_value):
# 读取文件内容并存储到字典中
data = {}
with open(file_path, "r") as file:
for line in file:
contract, angle = line.strip().split("|")
data[contract] = angle
# 检查字典中是否已存在相同的合约名字,如果存在且现在日增仓的值大于文件中的值,则更新日增仓的值;如果不存在,则追加写入新的合约名字和日增仓的值
if contract_name in data:
if float(increase_value) > float(data[contract_name]):
data[contract_name] = str(increase_value)
print("合约名字:", contract_name)
print("日增仓已更新为:", increase_value)
else:
print("合约名字:", contract_name)
print("日增仓未更新")
else:
data[contract_name] = str(increase_value)
print("合约名字:", contract_name)
print("日增仓已写入")
# 将最终的字典内容写入临时文件
with open(temp_file_path, "w") as file:
for contract, angle in data.items():
file.write(contract + "|" + angle + "\n")