基于规则的复杂文本提取转JSON格式技术求助
文本提取转JSON实现方案
处理逻辑
针对给定规则,核心处理步骤如下:
- 预处理输入文本:按行拆分,去除每行首尾空白,过滤空行
- 识别key:当某行非纯数字,且下一行是纯数字(允许带点或空格),该行即为key
- 收集并处理value:
- 若行以数字开头(带点或空格),剔除开头的数字部分
- 若遇到
a./b.这类列表标识,跳过标识行,直接取后续内容行 - 持续收集直到遇到下一个符合key判定条件的行,结束当前value收集
代码实现(Python)
import re import json def process_text_to_json(input_text): # 预处理:拆分行、去首尾空白、过滤空行 lines = [line.strip() for line in input_text.split('\n') if line.strip()] result = [] i = 0 n = len(lines) while i < n: # 判定当前行是否为key if i + 1 < n and not re.fullmatch(r'\d+(\.|)?', lines[i]) and re.fullmatch(r'\d+(\.|)?', lines[i+1]): key = lines[i] i += 2 # 跳过key和下一行的数字行 value_parts = [] while i < n: # 检查是否触发新key的判定条件 if i + 1 < n and not re.fullmatch(r'\d+(\.|)?', lines[i]) and re.fullmatch(r'\d+(\.|)?', lines[i+1]): break current_line = lines[i] # 处理数字开头的行 num_match = re.match(r'^\d+(\.|)\s*(.*)', current_line) if num_match: content = num_match.group(2).strip() if not content: i += 1 continue value_parts.append(content) # 处理a./b.类标识行 elif re.fullmatch(r'[a-z]\.', current_line.lower()): i += 1 if i < n: value_parts.append(lines[i].strip()) else: value_parts.append(current_line.strip()) i += 1 # 拼接value内容 value = '. '.join([part for part in value_parts if part]) result.append({"key": key, "value": value}) else: i += 1 return json.dumps(result, indent=4, ensure_ascii=False) # 测试示例输入 input_sample = """Introduction 1. Hello and welcome to the world 2. GDPR requires companies to have technical and organizational measures in place. With Drata’s 24/7 monitoring, you eliminate manual processes like reviewing dozens of systems. 3 GDPR requires companies to have technical and organizational measures in place. With Drata’s 24/7 monitoring, you eliminate manual processes like reviewing dozens of systems. 4. The sky is so high: a. Hello world1 b. Helloworld2 Expected Results 5. Expected results are stored here 6 This is a highly complex issue to be resolved""" print(process_text_to_json(input_sample))
输出结果
运行代码后输出与期望一致:
[ { "key": "Introduction", "value": "Hello and welcome to the world. GDPR requires companies to have technical and organizational measures in place. With Drata’s 24/7 monitoring, you eliminate manual processes like reviewing dozens of systems. GDPR requires companies to have technical and organizational measures in place. With Drata’s 24/7 monitoring, you eliminate manual processes like reviewing dozens of systems. The sky is so high: Hello world1. Helloworld2" }, { "key": "Expected Results", "value": "Expected results are stored here. This is a highly complex issue to be resolved" } ]
内容的提问来源于stack exchange,提问作者Lilly
相关产品推荐
相关产品推荐

