이 프롬프트를 개발자 콘솔에 복사하여 직접 시도해보세요!

내용
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(  # defaults to 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)