pandas 如何在Python dataframe中加载ODATA?

wf82jlnq  于 2023-06-28  发布在  Python
关注(0)|答案(2)|浏览(104)

我尝试从ODATA读取数据,然后使用Python创建 Dataframe 。
import requests import pyodata import json import pandas as pd

使用pyodata时
方法一

SERVICE_URL = 'https://xxx/sap/opu/odata/sap/Table_0001'

SERVICE_URL = 'https://xxx/opu/odata/Table_0001/?$format=json'

HTTP_LIB=requests.Session()
HTTP_LIB.auth = ('username', 'password')
HTTP_LIB.verify=False

我有以下问题:

<<bound method Service.http_get_odata of <pyodata.v2.service.Service object at 0x000001B491BD58D0>>

仅使用请求时****方法2

import pyodata
import requests
import json

r=requests.get('https://xxx/opu/odata/Table_0001/?$format=json', auth=('Username', 'Password'), verify=False)

print(r.status_code)

print(r.json())

我连接到ODATA,但我不能从表中选择数据,我有以下输出

{'d': {'EntitySets': ['Table_0001', 'AdditionalMetadata']}}

我尝试在Excel中加载ODATA,它正在工作。

laximzn5

laximzn51#

问题解决了
URL中提供的数据存在问题。
不知何故excel能够阅读它!
但是我确认方法2是完美的工作!

kmbjn2e3

kmbjn2e32#

我给了一个answer here
下面是一个示例代码:

import requests
import pyodata
import pandas as pd

SERVICE_URL = 'http://services.odata.org/V2/Northwind/Northwind.svc/'
HTTP_LIB = requests.Session()
northwind = pyodata.Client(SERVICE_URL, HTTP_LIB)

# query everything of entity set Costumers
costumers = northwind.entity_sets.Customers.get_entities().execute()

schema = service.schema
entity_type = next(et for et in schema.entity_types if et.name == "Costumer")
properties = entity_type.proprties()

def monta_prop_dict(obj, properties):
    d = {}
    for prop in properties:
        d[prop.name] = getattr(obj, prop.name)
    return d

data = []
for c in costumers:
    data.append(monta_prop_dict(c, properties))

df = pd.DataFrame(data)

相关问题