簡體   English   中英

TabPy:Python 將 Json 返回到 Tableau,“解析數字時出錯”

[英]TabPy: Python returning Json into Tableau, "Error Parsing Number"

我正在嘗試使用 Tableau 計算字段來使用我的 python 腳本,它獲取 JSON 數據。 我的最終目標是以表格格式將這些數據放入表格中。

我讀過 JSON 更容易進入與數據幀對象相反的畫面。

我目前在 Spyder 程序中使用它。 並執行此操作以獲取我的數據。

print (get1D ("2019-02-02", "2019-02-05", "Tableau", "Limits"))

在我的計算字段中,我收到錯誤:“Error Parsing Number”

.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)  
)  

請查看 Tableau 的在 Tableau 中創作 Python 計算的指南。

一般來說,格式需要是這樣的:

SCRIPT_INT("import xyz foo=_arg1+_arg2 return foo", SUM([A number]), SUM([Another Number])

據我所知,您需要在計算字段中添加引號,用 _argX 替換需要傳遞的任何字段變量,在計算字段中添加逗號,然后將字段列表作為這些參數傳遞。

請注意,只要您在 Tableau 計算字段窗口中看到錯誤“計算包含錯誤”,就表明問題出在 Tableau 計算字段(格式/編譯)中,而不一定與底層 Python 有關。 (您看到的錯誤是一條紅鯡魚。Tableau 計算字段解釋器將“.”視為小數,並希望在其后面收到一個數字。)在計算字段窗口中,Tableau 將無法檢查底層 Python - 它僅將其作為字符串傳遞給 TabPy。 反之亦然 - 在 Tableau 計算字段窗口中看到“此計算有效”並不一定意味着 Python 腳本將正確返回。

希望這可以幫助。

編輯以回應評論:

以下是使用您在問題中提供的代碼的示例。

  • 用單引號替換 Python 腳本中的雙引號。 通過這種方式,Tableau 將能夠區分自己的雙引號。 (Tableau 與 Python 的相同之處在於它對雙引號和單引號的處理方式相同。)
  • 將 getData() 的輸入參數替換為 _arg1 到 _arg4。
  • 在 Python 腳本作為字符串傳遞后,將 [開始日期]、[結束日期]、[節點名稱] 和 [類型] 作為參數傳遞。 (這些作為 _arg1 到 _arg4 被植入到字符串中。(ATTR() 在這里可能不是正確的聚合方法 - 您必須進行實驗。)
  • 計算字段現在至少可以編譯,但是,我不能保證它會在 Python 端執行或者它會完成你想要做的事情。
  • 我不確定 get1D() 會如何反應。 您可能也必須將 _arg1 到 _arg4 作為參數放在那里。 有一些工作需要完成 - 甚至可能需要重新格式化代碼以接受 Tableau args。

請務必閱讀 TabPy 文檔,以獲取有關其使用的更多說明,我無法在此處提供。 另外,這里有一篇很好的博客文章。 如果使用得當,它的功能非常強大。

祝你好運!

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 (_arg1, _arg2, _arg3, _arg4, 1)"
,
ATTR([Start Date]),ATTR([End Date]),ATTR([Node Name], ATTR([Type]
)  

為后代添加此答案並簡化我之前的答案和結果線程。

問題中提供的 Python 腳本旨在將數據直接加載到 Tableau 中。 TabPy 旨在對 Tableau 中已經存在的數據進行操作,而不是在攝取時作為源。

這里有必要將 Python 腳本的輸出放置到一個中間位置,然后 Tableau 可以連接到該位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM