Python中基于JSON的工单模板匹配逻辑实现求助
问题描述
- 现有如下JSON工单模板文件:
[ { "key":1, "subject": "Phone1 not found", "description": "The client's site is utilizing a 3rd party's DNI javascript. In order for us to track all calls, please ask the client to provide all possible DNI numbers. We have found the following numbers: " }, { "key":2, "subject": "Phone1 not matching", "description": "Phone1 is not matching. How it's going on" }, { "key":3, "subject": "Query Parameters", "description": "We have tried to set this campaign on tracking, but on capture site we are facing tracking issues. We noticed query string parameters are not getting accepted on clients original site.<br> Example : Query string should look like -https://www.nhtampabay.com?scid=xxxxxxx&cid=xxxxxxx. <br> Please ask the advertiser to pass the below query parameter through their server, <br>?scid=xxxxxxx&cid=xxxxxxx&tc=xxxxxxxxxx&rl_key=xxxxxxxxxxxxxxxxxxxxxxxxx&dynamic_proxy=1&primary_serv=www.xxxxxxxxxxxx.com&rl_track_landing_pages=1&rl_retarget=1 <br>This is original site issue and is not accepting our query string parameters for Reachcode functionality in the same way we have passed. Please ask advertiser to contact their web master to update the original site in such a way that it accepts our query string parameters in the same way it is expected. Reachcode site will work fine once the above said changes will be made on original site. The advertiser needs to allow our full query to pass through and not redirect." } ]
- 需要实现Python函数
ticketTemplate,功能要求:- 接收用户输入文本,用正则按
&或and(忽略前后空格)分割为splitresult列表 - 若
splitresult中的元素匹配JSON里某个条目的subject字段(比如包含"not found"),则将对应description加入matching_descriptions - 最终返回拼接后的描述或提示信息
- 接收用户输入文本,用正则按
- 以下是编写的存在逻辑问题的代码:
def ticketTemplate(myText): splitresult = re.split(r'\s*&\s*|\s*and\s*', myText) print(splitresult) matching_descriptions = [] with open('ticket_template.json', 'r') as myfile: data = myfile.read() obj = json.loads(data) for data1 in obj: if any(keyword in splitresult in data1["subject"] for keyword in ['not found', 'not present']): matching_descriptions.append(data1["description"]) if matching_descriptions: return '\n'.join(matching_descriptions) else: return "No Such Template Available. Please try again"
修复后的代码
import re import json def ticketTemplate(myText): # 按&或and(忽略前后空格)分割输入文本 splitresult = re.split(r'\s*(?:&|and)\s*', myText) # 过滤分割后可能出现的空字符串 splitresult = [item.strip() for item in splitresult if item.strip()] matching_descriptions = [] # 读取并解析JSON模板文件 with open('ticket_template.json', 'r') as myfile: templates = json.load(myfile) # 遍历每个工单模板,检查匹配关系 for template in templates: subject = template["subject"] for item in splitresult: if item in subject: matching_descriptions.append(template["description"]) break # 匹配到就停止当前模板的检查,避免重复添加 if matching_descriptions: return '\n'.join(matching_descriptions) else: return "No Such Template Available. Please try again"
关键修复说明
- 正则与分割结果优化:原正则分割后会产生空字符串,调整正则并添加过滤逻辑,确保
splitresult里都是有效内容 - 匹配逻辑修正:原代码的
any(...)语法错误且逻辑混乱,改为遍历分割后的每个元素,检查是否存在于模板的subject中,匹配成功就加入对应描述 - JSON读取简化:直接用
json.load()读取文件,省去先读字符串再解析的步骤,更高效 - 避免重复匹配:每个模板匹配到有效元素后就停止检查,防止同一描述被多次添加
内容的提问来源于stack exchange,提问作者sameer
相关产品推荐
相关产品推荐

