117 lines
3.6 KiB
Python
117 lines
3.6 KiB
Python
|
import json
|
|||
|
import re
|
|||
|
|
|||
|
def extract_content_from_json(json_data):
|
|||
|
"""提取 { 和 } 之间的内容,并将其解析为字典"""
|
|||
|
if not json_data.strip():
|
|||
|
return {}
|
|||
|
match = re.search(r'\{[\s\S]*\}', json_data)
|
|||
|
if match:
|
|||
|
try:
|
|||
|
json_data = match.group(0)
|
|||
|
return json.loads(json_data) #返回字典
|
|||
|
except json.JSONDecodeError as e:
|
|||
|
print(f"JSON decode error: {e}")
|
|||
|
return {}
|
|||
|
else:
|
|||
|
print("No valid JSON content found.")
|
|||
|
return {}
|
|||
|
|
|||
|
def clean_json_string(json_string):
|
|||
|
"""清理JSON字符串,移除多余的反引号并解析为字典"""
|
|||
|
return extract_content_from_json(json_string)
|
|||
|
|
|||
|
def combine_json_results(json_lists):
|
|||
|
"""
|
|||
|
将类json格式的列表整合成json数据(即大括号{}包裹)
|
|||
|
"""
|
|||
|
combined_result = {}
|
|||
|
for json_str in json_lists:
|
|||
|
if json_str.strip():
|
|||
|
json_data = clean_json_string(json_str)
|
|||
|
combined_result.update(json_data)
|
|||
|
return combined_result
|
|||
|
|
|||
|
|
|||
|
def nest_json_under_key(data, key):
|
|||
|
"""
|
|||
|
将给定的字典 data 嵌套在一个新的字典层级下,该层级由 key 指定,并返回 JSON 格式的字符串。
|
|||
|
|
|||
|
参数:
|
|||
|
- data: dict, 要嵌套的原始字典。
|
|||
|
- key: str, 新层级的键名。
|
|||
|
|
|||
|
返回:
|
|||
|
- 嵌套后的 JSON 字符串。
|
|||
|
"""
|
|||
|
# 创建一个新字典,其中包含一个键,该键的值是原始字典
|
|||
|
nested_dict = {key: data}
|
|||
|
# 将字典转换成 JSON 字符串
|
|||
|
nested_json = json.dumps(nested_dict, ensure_ascii=False, indent=4)
|
|||
|
return nested_json
|
|||
|
|
|||
|
|
|||
|
def add_keys_to_json(target_dict, source_dict):
|
|||
|
"""
|
|||
|
将 source_dict 的内容添加到 target_dict 中的唯一外层键下的字典中。
|
|||
|
|
|||
|
参数:
|
|||
|
target_dict (dict): 要更新的目标字典,假定只有一个外层键。
|
|||
|
source_dict (dict): 源字典,其内容将被添加到目标字典。
|
|||
|
|
|||
|
返回:
|
|||
|
dict: 更新后的字典。
|
|||
|
"""
|
|||
|
if not target_dict:
|
|||
|
print("Error: Target dictionary is empty.")
|
|||
|
return {}
|
|||
|
|
|||
|
if len(target_dict) != 1:
|
|||
|
print("Error: Target dictionary must contain exactly one top-level key.")
|
|||
|
return target_dict
|
|||
|
|
|||
|
# 获取唯一的外层键
|
|||
|
target_key, existing_dict = next(iter(target_dict.items()))
|
|||
|
|
|||
|
if not isinstance(existing_dict, dict):
|
|||
|
print(f"Error: The value under the key '{target_key}' is not a dictionary.")
|
|||
|
return target_dict
|
|||
|
|
|||
|
# 合并字典
|
|||
|
existing_dict.update(source_dict)
|
|||
|
|
|||
|
# 更新原字典
|
|||
|
target_dict[target_key] = existing_dict
|
|||
|
|
|||
|
return target_dict
|
|||
|
|
|||
|
|
|||
|
def rename_outer_key(original_data,new_key):
|
|||
|
# 定义新的键名
|
|||
|
# new_key = "重新招标, 不再招标和终止招标"
|
|||
|
|
|||
|
# 提取原始数据中的唯一外层值(假设只有一个外层键)
|
|||
|
if not original_data or not isinstance(original_data, dict):
|
|||
|
return {} # 如果输入无效或不是字典,则返回空字典
|
|||
|
|
|||
|
# 使用 next(iter(...)) 提取第一个键的值
|
|||
|
original_value = next(iter(original_data.values()), {})
|
|||
|
|
|||
|
# 创建一个新的字典,使用新的键名
|
|||
|
new_data = {new_key: original_value}
|
|||
|
|
|||
|
return json.dumps(new_data,ensure_ascii=False)
|
|||
|
def transform_json_values(data):
|
|||
|
if isinstance(data, dict):
|
|||
|
return {key: transform_json_values(value) for key, value in data.items()}
|
|||
|
elif isinstance(data, list):
|
|||
|
return [transform_json_values(item) for item in data]
|
|||
|
elif isinstance(data, bool):
|
|||
|
return '是' if data else '否'
|
|||
|
elif isinstance(data, (int, float)):
|
|||
|
return str(data)
|
|||
|
elif isinstance(data, str):
|
|||
|
return data.replace('\n', '<br>')
|
|||
|
else:
|
|||
|
return data
|