我正在尝试使用 Tableau 计算字段来使用我的 python 脚本,该脚本获取 JSON 数据。我的最终目标是以表格格式将这些数据放入画面中。
我读过 JSON 更容易进入与数据框对象相反的画面。
我目前在 Spyder 程序中使用它。并执行此操作以获取我的数据。
print (get1D ("2019-02-02", "2019-02-05", "Tableau", "Limits"))
在我的计算字段中,我收到错误:“错误解析数”
.format(status_code))
错误信息:
将这些数据放入画面中的任何帮助将不胜感激。这是我的完整脚本。
SCRIPT_INT(
import time
import requests
import json
import pandas as pd
import re
import urllib3
import math
from io import StringIO
from datetime import datetime, date,timedelta
from pandas.tseries.offsets import BDay
from urllib.parse import urlencode
from flask import json
def call_api(url, request_dict, post):
if post:
header = {'content-type':'application/json'}
resp = requests.post(url, data=json.dumps(request_dict), auth=('user', 'pass'), headers = header, verify=False)
else:
url = url + urlencode(request_dict)
resp = requests.get(url, auth=('user', 'pass'), verify=False)
status_code = resp.status_code
if status_code == 401:
raise ValueError("There is an error with the connection.\nLogin failed. \nNot authorized. Please check your credentials and try again.\nStatus code {}".format(status_code))
elif status_code == 404:
raise ValueError("There is an error with the connection.\nCould not connect to the server.\nStatus code {}".format(status_code))
elif status_code == 200:
pass
else:
raise ValueError("There is an error with the connection.\nStatus code {}".format(status_code))
return resp
def getData (startDate, endDate, nodeName, Type, Id):
request_dict = [{
"hierarchy": "Tableau",
"nodeName": nodeName,
"FilterId": Type,
"Id": Id ,
}]
url = "https://sampleurl/startDate={0}&endDate={1}"
startDate = datetime.strptime(startDate, '%Y-%m-%d')
startDate = startDate.strftime ('%Y%m%d')
endDate = datetime.strptime(endDate, '%Y-%m-%d')
endDate = endDate.strftime ('%Y%m%d')
url = url.format(startDate, endDate)
resp = call_api(url, request_dict, True)
return resp.json ()
def get1D(startDate, endDate, nodeName, Type):
return getData (startDate, endDate, nodeName, Type, 1)
)