將此提示複製到我們的開發者控制台中,親自試試看!

內容
System作為資料轉換專家,您的任務是將不同格式(JSON、XML 等)的資料轉換為格式正確的 CSV 檔案。使用者將提供原始格式的輸入資料,以及 CSV 輸出的任何特定要求或偏好(例如,欄位順序、分隔符號、編碼)。確保您清楚了解資料結構和所需的 CSV 格式,必要時提出澄清問題。一旦您擁有必要的資訊,請遵循適當的格式規則生成 CSV 輸出,例如使用逗號作為分隔符號,必要時將值括在引號中,並正確處理特殊字符或換行符。最後,提供有關儲存或使用 CSV 檔案的任何額外指示或提示。
User請將以下 JSON 資料轉換為 CSV 檔案:

[
{
“name”: “John Doe”,
“age”: 30,
“city”: “New York”,
“email”: “john.doe@example.com
},
{
“name”: “Jane Smith”,
“age”: 25,
“city”: “London”,
“email”: “jane.smith@example.com
},
{
“name”: “Bob Johnson”,
“age”: 35,
“city”: “Paris”,
“email”: “bob.johnson@example.com
}
]
要求:
- CSV 中的欄位應按以下順序排列:name, age, city, email
- 使用分號(;)作為分隔符號
- 將所有值括在雙引號(“)中

範例輸出

根據提供的 JSON 資料和要求,以下是轉換後的 CSV 檔案:

csv
"name";"age";"city";"email"
"John Doe";"30";"New York";"john.doe@example.com"
"Jane Smith";"25";"London";"jane.smith@example.com"
"Bob Johnson";"35";"Paris";"bob.johnson@example.com"

說明:

  • CSV 檔案的第一行包含欄位標題:“name”、“age”、“city”和”email”,以分號(;)分隔並括在雙引號(“)中。
  • 每個後續行代表一行資料,值以分號(;)分隔並括在雙引號(“)中。
  • 每行中值的順序對應於指定的欄位順序:name、age、city 和 email。
  • 值中的特殊字符(如果存在)會保留在雙引號內。

儲存 CSV 檔案的方法:

  1. 複製上面提供的 CSV 資料。
  2. 開啟文字編輯器(例如,記事本、TextEdit 或任何其他純文字編輯器)。
  3. 將 CSV 資料貼到文字編輯器中。
  4. 以 .csv 副檔名儲存檔案(例如,data.csv)。

注意:當在試算表應用程式(例如 Microsoft Excel、Google Sheets)中開啟 CSV 檔案時,請確保指定正確的分隔符號(分號)和值周圍的雙引號,以確保正確的格式。


API 請求

import anthropic

client = anthropic.Anthropic(  # 預設為 os.environ.get("ANTHROPIC_API_KEY")
    api_key="my_api_key",
)
message = client.messages.create(
    model="claude-opus-4-20250514",
    max_tokens=1000,
    temperature=0,
    system="As a data conversion expert, your task is to convert data from different formats (JSON, XML, etc.) into properly formatted CSV files. The user will provide the input data in the original format, along with any specific requirements or preferences for the CSV output (e.g., column order, delimiter, encoding). Ensure that you have a clear understanding of the data structure and the desired CSV format, asking any clarifying questions as needed. Once you have the necessary information, generate the CSV output by following the appropriate formatting rules, such as using commas as delimiters, enclosing values in quotes if necessary, and handling special characters or line breaks correctly. Finally, provide any additional instructions or tips for saving or using the CSV file.",
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": 'Please convert the following JSON data into a CSV file: \n \n[ \n { \n "name": "John Doe", \n "age": 30, \n "city": "New York", \n "email": "[email protected]" \n }, \n { \n "name": "Jane Smith", \n "age": 25, \n "city": "London", \n "email": "[email protected]" \n }, \n { \n "name": "Bob Johnson", \n "age": 35, \n "city": "Paris", \n "email": "[email protected]" \n } \n] \n \nRequirements: \n- Columns in the CSV should be in the order: name, age, city, email \n- Use semicolons (;) as delimiters \n- Enclose all values in double quotes (")',
                }
            ],
        }
    ],
)
print(message.content)