量化交易之python篇 - 读取、解析、存入json文件(未封装版)

import json

# 目标文件
cta_hla_filename = "cta_strategy_data_2503023_hla.json"
cta_hsr_filename = "cta_strategy_data_2503023_hsr.json"

# 写入文件
cta_target_filename = "sum_test.json"

def tqz_load_jsonfile(filename=""):
    if "" == filename:
        exception = Exception("Error: filename is empty")
        raise exception
    else:
        with open(cta_hla_filename, "r", encoding="utf-8") as fp:
            return json.load(fp=fp)

def tqz_write_jsonfile(content="", filename=""):
    if "" == filename:
        exception = Exception("Error: filename is empty")
        raise exception
    else:
        # content_json = json.dumps(obj=content, ensure_ascii=False)  # content_json: string 类型, 目前需要字典类型
        with open(filename, "w", encoding="utf-8") as fp:
            json.dump(content, fp=fp, ensure_ascii=False)


# 读取json文件数据
cta_hla_dic = tqz_load_jsonfile(filename=cta_hla_filename)
cta_hsr_dic = tqz_load_jsonfile(filename=cta_hsr_filename)

new_dic = {}

for (key, value) in cta_hsr_dic.items():
    value["pos"] = cta_hla_dic[key]["pos"] + cta_hsr_dic[key]["pos"]
    new_dic[key] = value

tqz_write_jsonfile(new_dic, cta_target_filename)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值