简体   繁体   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

I work for a property company and am trying to create a frequently updated excel sheet with all of company's data on units/tenants/properties/etc.我在一家房地产公司工作,正在尝试创建一个经常更新的 excel 表,其中包含公司关于单元/租户/属性/等的所有数据。 We currently make use of a property management site which contains all of our data.我们目前使用包含我们所有数据的物业管理网站。 The site has an api which I should be able to use to access the data.该站点有一个 api 我应该可以用来访问数据。

I have tried importing the data from the site's api, and have been successful in importing it.我已经尝试从站点的api 导入数据,并且已经成功导入。 With that being said, the imported data is only in the format of a json file and I have been having trouble converting it to a csv.话虽如此,导入的数据仅采用 json 文件的格式,我在将其转换为 csv 时遇到了麻烦。

Here is my current program.这是我目前的程序。

'''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")
    

If I print the imported json dictionary before converting it to a csv, this is a sample of what the current structure of it looks like.如果我在将导入的 json 字典转换为 csv 之前打印它,这是它当前结构的示例。

{'results': [{'Property': '1020p - 1024 N. roadname Pkwy. {'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', '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', '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, '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', 'Rent': '1,075.00', 'MoveIn': '10/17/2021', 'MoveOut': None}], 'next_page_url': None} 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':无}

I believe that due to how the API works, it is making a nested dictionary with the first key being labelled results , and the final key being labelled next_page_url .我相信,由于 API 的工作方式,它正在制作一个嵌套字典,其中第一个键被标记为results ,最后一个键被标记为next_page_url

Due to this, I believe that when converting the dictionary to a csv with pandas, it is putting all my keys that have to deal with property data in their own column.因此,我相信在将字典转换为 csv 和 pandas 时,它会将我所有必须处理属性数据的键放在自己的列中。 And this is something that I would like to change.这是我想改变的。 My current converted csv output looks like this.我当前转换的 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]

Ideally here, each column from the api such as PropertyName, Tenant, PropertyAddress, etc. would all have their own columns in the csv.理想情况下,api 中的每一列(例如 PropertyName、Tenant、PropertyAddress 等)在 csv 中都有自己的列。 Could anyone tell me how to go about formatting it that way?谁能告诉我如何用 go 格式化它?

Thank you!谢谢!

Do you need next_page_url in the final CSV?最终的 CSV 中需要next_page_url吗?

Did you try doing this?你试过这样做吗?

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

I had trouble with the formatting used in that.json example, but you actually can solve this an easier way using the json library you have imported.我在该示例中使用的格式有问题。json 示例,但您实际上可以使用您导入的 json 库以更简单的方式解决此问题。

I usually go about it by saving the request response:我通常通过保存请求响应 go 关于它:

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

Then I use the.loads() function which loads it into a python dict, which then you can index.然后我使用 .loads() function 将其加载到 python 字典中,然后您可以对其进行索引。

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

will give you会给你

[{'Property': '1020p - 1024 N. roadname Pkwy. [{'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', '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', '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, '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', 'Rent': '1,075.00', 'MoveIn': '10/17/2021', 'MoveOut': None}] Cityname, XX 12345', 'Unit': '100', 'Tenant': 'John Roberts', 'BdBa': '1/1.00', 'SquareFt': '930', 'MarketRent': '1,075.00', '租金:'1,075.00','搬入':'10/17/2021','搬出':无}]

Then, since the columns have the same name, its as easy as using pandas to create a df, then convert it to a csv.然后,由于列具有相同的名称,它就像使用 pandas 创建一个 df,然后将其转换为 csv 一样简单。

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

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

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