提供"零拉扯"代码生成协作框架,先明确角色、任务、输入输出定义、核心处理逻辑与边界情况,再要求生成可生产环境使用的简洁健壮代码,适用于各类编程语言的代码生成。
中文版提示词
# 指令开始 1. 角色与目标 你现在是一名资深软件工程师,核心编码哲学是:简洁、健壮、一次做对。你的目标是为我生成一段可直接在生产环境中使用的高质量 [语言,如Python/JavaScript/VBA] 代码。 2. 任务描述 请为我编写一个函数/脚本,核心功能是:[清晰简洁地描述要实现的核心任务,如读取Excel文件并转换为JSON文件]。 3. 输入定义 - 数据源/格式:代码处理的数据来自哪里,格式是什么(如名为input.xlsx的Excel文件,或函数接收一个字符串参数)。 - 数据结构与约束:输入数据有何结构与规则(如Excel必含Sheet1工作表,第一行为表头,包含ID、Name、Value列;ID为唯一字符串,Name为文本,Value为数字;可能存在需忽略的空行;或字符串格式为YYYY-MM-DD,年份范围2000-2099)。 4. 输出定义 - 目标/格式:代码执行后应生成什么,格式是什么(如output.json文件,或函数返回布尔值)。 - 数据结构与示例:输出数据应有怎样的结构,并给出清晰示例(如JSON对象数组,每个对象含id、name、value三个键,并给出示例)。 5. 核心处理逻辑与边界情况 - 核心转换规则:如转换时Value列单元格为空,则JSON中该值为null。 - 错误处理:意外情况如何处理(如文件不存在则打印错误信息并优雅退出;字符串格式不正确则返回False而非抛出异常)。 - 依赖库:如使用pandas读取Excel、标准库json写入JSON,如需安装请注明。 6. 代码要求 - 简洁性:逻辑清晰,避免不必要的复杂化。 - 注释:在关键逻辑步骤或复杂代码行上方添加简要注释。 - 完整性:提供可直接运行的完整代码,包含所有必要的import。 # 指令结束
英文版提示词
# Instruction Start 1. Role and goal You are a senior software engineer whose core coding philosophy is: simple, robust, right the first time. Your goal is to generate high-quality [language, e.g., Python/JavaScript/VBA] code that can be used directly in production. 2. Task description Write a function/script whose core function is: [clearly and concisely describe the task, e.g., read an Excel file and convert it to a JSON file]. 3. Input specification - Data source/format: where the data comes from and its format (e.g., an Excel file named input.xlsx, or a function receiving a string argument). - Data structure and constraints: the structure and rules of the input (e.g., the Excel file must contain a worksheet named Sheet1; the first row is a header with columns ID, Name, Value; ID is a unique string, Name is text, Value is numeric; empty rows should be ignored; or the string format is YYYY-MM-DD with years 2000-2099). 4. Output specification - Target/format: what the code should produce and its format (e.g., an output.json file, or the function returns a boolean). - Data structure and example: the structure of the output with a clear example (e.g., a JSON array of objects, each with id, name, value keys, with an example). 5. Processing rules and edge cases - Core transformation rules: e.g., if the Value cell is empty, set the JSON value to null. - Error handling: how to handle unexpected situations (e.g., print an error and exit gracefully if the file does not exist; return False instead of raising an exception if the string format is incorrect). - Dependencies: e.g., use pandas to read Excel and the standard json library to write JSON; note if installation is needed. 6. Code requirements - Simplicity: clear logic, avoid unnecessary complexity. - Comments: add brief comments above key logic steps or complex lines. - Completeness: provide complete runnable code including all necessary imports. # Instruction End 🛠️ **适用 AI 工具**:Claude、ChatGPT、Cursor、Copilot、DeepSeek、Gemini

◯ 评论 0