0

パーソナリティ インサイト サービス アプリを Bluemix にデプロイしたので、post コマンドを呼び出してテキストを送信できました。データの出力を csv に保存したいのですが、Personality API の API ドキュメントを確認しましたが、どこが間違っているのか理解できませんでした。出力をcsvファイルに保存するのを手伝ってくれる人はいますか。

Pythonで使用したコードは次のとおりです。

#!/usr/bin/env python
# coding: utf-8

import requests
import json
import csv

response = requests.post("https://gateway.watsonplatform.net/personality-insights/api/v2/profile",
                         auth=("user-id", "password"),
                         headers={"content-type": "application/json", "Accept": "text/csv"},
                         data = "text to be analyzed"
                         )

jsonProfile = json.loads(response.text)

with open('test1234.csv', 'w') as f:
    for row in jsonProfile:
        f.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)

これについて私を助けてください。

4

1 に答える 1

2

You are already getting CSV data from the request, with the Accept: text/csv request header. All you need is to output this directly to a file, no need of using any library (Personality Insights has already formatted this for you!).

Here is a modified code that works:

response = requests.post("https://gateway.watsonplatform.net/personality-insights"+
                         "/api/v2/profile?headers=True",
     auth = ("userid", "password"),
     headers = {"content-type": "text/plain", "Accept": "text/csv"},
     data = "replace your text here"
     )
with open('personality-insights.csv', 'w') as f:
    print >> f, response.text

Also note the addition of the ?headers=True as query parameter in the URL: this will output the column names -- you will likely find this useful (If you are concatenating outputs of several API calls, then omit this parameter and just get the values as in your original example).

于 2015-06-07T21:35:04.487 に答える