繁体   English   中英

Importing API json data and converting to a csv for export to Excel in python

[英]Importing API json data and converting to a csv for export to Excel in python

我在一家房地产公司工作,正在尝试创建一个经常更新的 excel 表,其中包含公司关于单元/租户/属性/等的所有数据。 我们目前使用包含我们所有数据的物业管理网站。 该站点有一个 api 我应该可以用来访问数据。

我已经尝试从站点的api 导入数据,并且已经成功导入。 话虽如此,导入的数据仅采用 json 文件的格式,我在将其转换为 csv 时遇到了麻烦。

这是我目前的程序。

'''all imports needed'''
import requests
import pandas as pd
import csv
import json

#get data from api
url ='https://*apiusername:apisecretkey@companyname*.appfolio.com/api/v1/reports/rent_roll.json?columns=Property,PropertyName,PropertyGroupId,PropertyAddress,Unit,Tenant,BdBa,SquareFt,MarketRent,Rent,MoveIn,MoveOut'

try:
    response = requests.get(url).json()
    print("successfully imported json data from appfolio api.")

except IOError:
    print("I/O Error")


#flatten json dictionary just in case
def flattenjson(b, delim):
    print("attempting to flatten json dictionary.")
    val = {}
    for i in b.keys():
        if isinstance(b[i], dict):
            get = flattenjson(b[i], delim)
            for j in get.keys():
                val[i+ delim + j]= get[j]

        else:
            val[i] = b[i]

    return val

test = flattenjson(response, ',')

#print output for test
print(test)

#confirm that test variable is in dictionary format
if isinstance(test, dict):
    print("your imported file is a dictionary.")


#convert dictionary to a csv with pandas
try:
    
    df = pd.DataFrame.from_dict(test)
    print("converted dictionary to pandas dataframe.\n\n")

except:
    print("Error")

try:
    
    df.to_csv("data.csv")
    print("successfully converted dataframe to csv file. attempting to read back data.\n\n")
    df = pd.read_csv("data.csv", index_col=0)
    print(df)

except:
    print("Error")
    

如果我在将导入的 json 字典转换为 csv 之前打印它,这是它当前结构的示例。

{'results': [{'Property': '1020p - 1024 N. roadname Pkwy. Cityname, XX 12345', 'PropertyName': '1020p', 'PropertyGroupId': '418024, 418031, 418057, 418068, 418069, 418073, 418077', 'PropertyAddress': '1020 N. roadname Pkwy。 Cityname, XX 12345', 'Unit': 'Commercial- Loop Lofts Unit B', 'Tenant': None, 'BdBa': '--/--', 'SquareFt': '4,888', 'MarketRent': ' 4,000.00', 'Rent': None, 'MoveIn': None, 'MoveOut': None}, {'Property': '1020p - 1024 N. roadname Pkwy. Cityname, XX 12345, 'PropertyName': '1020p', 'PropertyGroupId': '418024, 418031, 418057, 418068, 418069, 418073, 418077', 'PropertyAddress': '1020 N. roadname Pkwy. Cityname, XX 12345', 'Unit': '100', 'Tenant': 'John Roberts', 'BdBa': '1/1.00', 'SquareFt': '930', 'MarketRent': '1,075.00', '租金:'1,075.00','搬入':'10/17/2021','搬出':无}],'next_page_url':无}

我相信,由于 API 的工作方式,它正在制作一个嵌套字典,其中第一个键被标记为results ,最后一个键被标记为next_page_url

因此,我相信在将字典转换为 csv 和 pandas 时,它会将我所有必须处理属性数据的键放在自己的列中。 这是我想改变的。 我当前转换的 csv output 看起来像这样。

    results  next_page_url                      
 0    {'Property': '1020p - 1024 N. roadname Pkwy. St...           NaN
 1    {'Property': '1020p - 1024 N. roadname Pkwy. St...           NaN
 2    {'Property': '1020p - 1024 N. roadname Pkwy. St...           NaN
 3    {'Property': '1020p - 1024 N. roadname Pkwy. St...           NaN
 4    {'Property': '1020p - 1024 N. roadname Pkwy. St...           NaN
 ..                                                 ...            ...
 639  {'Property': 'putinvest - 4240 something Ave....             NaN
 640  {'Property': 'putmgr - 4240 something Ave. St...             NaN
 641  {'Property': 'z4184p - 4184 Something Ave. Jo...             NaN
 642  {'Property': 'z4400p - 4400 Boardwalk Name  St. ...          NaN
 643  {'Property': 'z4514 - 4514 something Ave. St. Lo...          NaN
 
 [644 rows x 2 columns]

理想情况下,api 中的每一列(例如 PropertyName、Tenant、PropertyAddress 等)在 csv 中都有自己的列。 谁能告诉我如何用 go 格式化它?

谢谢!

最终的 CSV 中需要next_page_url吗?

你试过这样做吗?

df = pd.DataFrame(test["results"])

我在该示例中使用的格式有问题。json 示例,但您实际上可以使用您导入的 json 库以更简单的方式解决此问题。

我通常通过保存请求响应 go 关于它:

response = requests.request("GET", new_url, headers=headers, data=payload)

然后我使用 .loads() function 将其加载到 python 字典中,然后您可以对其进行索引。

results = json.loads(response.text)['results'][0]

会给你

[{'Property': '1020p - 1024 N. roadname Pkwy. Cityname, XX 12345', 'PropertyName': '1020p', 'PropertyGroupId': '418024, 418031, 418057, 418068, 418069, 418073, 418077', 'PropertyAddress': '1020 N. roadname Pkwy。 Cityname, XX 12345', 'Unit': 'Commercial- Loop Lofts Unit B', 'Tenant': None, 'BdBa': '--/--', 'SquareFt': '4,888', 'MarketRent': ' 4,000.00', 'Rent': None, 'MoveIn': None, 'MoveOut': None}, {'Property': '1020p - 1024 N. roadname Pkwy. Cityname, XX 12345, 'PropertyName': '1020p', 'PropertyGroupId': '418024, 418031, 418057, 418068, 418069, 418073, 418077', 'PropertyAddress': '1020 N. roadname Pkwy. Cityname, XX 12345', 'Unit': '100', 'Tenant': 'John Roberts', 'BdBa': '1/1.00', 'SquareFt': '930', 'MarketRent': '1,075.00', '租金:'1,075.00','搬入':'10/17/2021','搬出':无}]

然后,由于列具有相同的名称,它就像使用 pandas 创建一个 df,然后将其转换为 csv 一样简单。

pd.DataFrame(results).to_csv('~/filename.csv')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM