Commit 5f50db57 authored by stephen.wang's avatar stephen.wang

1. api_json_save.http: 將api以json儲存至本地

2. recomm_reason.py: 配置說明更新至json中
parent 3bf6ce4b
### 请求 8080 lowrisk_rr3 -> AB088E61-FAB1-4466-B6AD-6E8AE253391E
GET http://localhost:8081/franklin/recommend/save
### 请求 8081 usmarket_prr4 -> 2CB50688-96F8-4C3E-87AB-6DEE2B4713B1
GET http://localhost:8081/franklin/recommend/save
### 请求 8082 industryfund -> D0FE0BF1-14F8-4350-833C-FD77AEE73E7A
GET http://localhost:8082/franklin/recommend/save
\ No newline at end of file
import json
import pandas as pd
def recomm_reason(filelist, note_review, printoutput=False):
'''
:param filelist: 投资组合guid的名称列表
:param note_review: 投资组合的配置说明
:param printoutput: 是否打印输出结果
'''
print('------------------------------------------------------------------------------------------')
for file_name in filelist:
# 读取原json文件
try:
with open(f'{file_name}.json', 'r', encoding='utf-8') as file:
data = json.load(file)
except FileNotFoundError:
print(f'{file_name}.json文件未找到,请检查文件路径。')
except json.JSONDecodeError:
print(f'{file_name}.json文件不是有效的 JSON 格式,请检查文件内容。')
# 提取 note 字段中的 recomm_reason 内容
import ast
note_dict = ast.literal_eval(data[0]['data']['note'])
# 更新 recomm_reason 内容
data_date = data[0]['data']['data_date']
try:
new_reason = note_review[
(note_review['rebalance_date'] == data_date) & (note_review['portfolio_guid'] == file_name)][
'note_review'].iloc[0]
note_dict['recomm_reason'] = new_reason
except IndexError:
print(f'{file_name}: rebalance_date or portfolio_guid does not match.')
continue
# 更新 note 字段
data[0]['data']['note'] = json.dumps(note_dict, ensure_ascii=False)
# 输出修改后的 JSON 数据,指定编码为 UTF - 8
if printoutput: print(json.dumps(data, indent=2, ensure_ascii=False).encode('utf-8').decode())
# 保存修改后的 JSON 数据到新文件
try:
with open(f'{file_name}_{data_date}.json', 'w', encoding='utf-8') as file:
json.dump(data, file, indent=2, ensure_ascii=False)
print(f'修改后的 JSON 数据已成功保存到 [{file_name}_{data_date}.json] 文件中。')
except Exception as e:
print(f'保存文件时出现错误: {e}')
print('------------------------------------------------------------------------------------------')
if __name__ == '__main__':
# 參數設置
filelist = ['D0FE0BF1-14F8-4350-833C-FD77AEE73E7A']
note_review = pd.read_excel('主題式投資組合配置說明_0425v1(正式送審).xlsx', sheet_name='note_review')
# 更新說明
recomm_reason = recomm_reason(filelist, note_review)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment