Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Paginate API in python
#2
You are using a writer to write dictionaries. You should be using a DictWriter. The writer is iterating through your dictionaries to write the rows, and when you iterate through a dictionary you get the keys, not the values.

Using the link provided in you post I wrote this:
import json
import csv
import pandas as pd

response = """{
    "totalRecords": 992,
    "maxPageSize": 100,
    "currentPageSize": 1,
    "nextPageLink": null,
    "nextCursor": 12935,
    "data": [
        {
            "id": 12729,
            "checklistName": "Signature",
            "description": "Signature",
            "user": "Wagner Robin",
            "userId": 3196,
            "checklistDate": "03-01-2020",
            "accountCode": "DM24"
        }
   ]
}"""

response = json.loads(response)
data = [response["data"]]

with open('data.csv','w',encoding='UTF8', newline='')as f:    
    writer = csv.DictWriter(f, data[0][0].keys())
    writer.writeheader()
    for page in data:
        writer.writerows(page)

df = pd.read_csv("data.csv")
print(df)
Which produces this output:
Output:
id checklistName description user userId checklistDate accountCode 0 12729 Signature Signature Wagner Robin 3196 03-01-2020 DM24
Reply


Messages In This Thread
Paginate API in python - by shantanu97 - Sep-01-2022, 03:02 AM
RE: Paginate API in python - by deanhystad - Sep-01-2022, 06:00 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020