-2

Given this dictionary:

{
    "last_id": "9095247150673486907",
    "stories": [
        {
            "description": "The $68.7 billion deal would be Microsoft\u2019s biggest takeover ever and the biggest deal in video game history. The acquisition would make Microsoft the world\u2019s third-largest gaming company by revenue,\u2026 The post Following the takeover of Activision by Microsoft, Sony is already being shaken up appeared first on The Latest News.",
            "favicon_url": "https://static.tickertick.com/website_icons/gettotext.com.ico",
            "id": "5310290716350155140",
            "site": "gettotext.com",
            "tags": [
                "msft"
            ],
            "time": 1642641278000,
            "title": "Following the takeover of Activision by Microsoft, Sony is already being shaken up",
            "url": "https://gettotext.com/following-the-takeover-of-activision-by-microsoft-sony-is-already-being-shaken-up/"
        },
        {
            "description": "Also Read | Acquisition of Activision Blizzard by Microsoft: an opportunity born out of chaos An announcement of such a nature could only inspire a good number of analysts, whose\u2026 The post Microsoft\u2019s takeover of Activision Blizzard ignites analysts appeared first on The Latest News.",
            "favicon_url": "https://static.tickertick.com/website_icons/gettotext.com.ico",
            "id": "-14419799692027457",
            "site": "gettotext.com",
            "tags": [
                "msft"
            ],
            "time": 1642641042000,
            "title": "Microsoft\u2019s takeover of Activision Blizzard ignites analysts",
            "url": "https://gettotext.com/microsofts-takeover-of-activision-blizzard-ignites-analysts/"
        },
        {
            "description": "Practical in-ears, mini speakers with long battery life or powerful boom boxes \u2013 the manufacturer Anker offers a suitable product for almost every situation. On Ebay and Amazon you can\u2026 The post Anker on Ebay and Amazon on offer: Inexpensive Soundcore 3, Motion Boom & Co appeared first on The Latest News.",
            "favicon_url": "https://static.tickertick.com/website_icons/gettotext.com.ico",
            "id": "5221754710166764872",
            "site": "gettotext.com",
            "tags": [
                "amzn"
            ],
            "time": 1642640469000,
            "title": "Anker on Ebay and Amazon on offer: Inexpensive Soundcore 3, Motion Boom & Co",
            "url": "https://gettotext.com/anker-on-ebay-and-amazon-on-offer-inexpensive-soundcore-3-motion-boom-co/"
        },
        {
            "favicon_url": "https://static.tickertick.com/website_icons/trib.al.ico",
            "id": "-3472956334378244458",
            "site": "trib.al",
            "tags": [
                "goog"
            ],
            "time": 1642640285000,
            "title": "Google is forming a group dedicated to blockchain and related technologies under a newly appointed executive",
            "url": "https://trib.al/nZz3omw"
        },
        {
            "description": "Texas' attorney general on Wednesday sued Google, alleging the company asked local radio DJs to record personal endorsements for smartphones that they hadn't used or been provided.",
            "favicon_url": "https://static.tickertick.com/website_icons/yahoo.com.ico",
            "id": "9095247150673486907",
            "site": "yahoo.com",
            "tags": [
                "goog"
            ],
            "time": 1642639680000,
            "title": "Texas sues Google over local radio ads for its smartphones",
            "url": "https://finance.yahoo.com/m/b44151c6-7276-30d9-bc62-bfe18c6297be/texas-sues-google-over-local.html?.tsrc=rss"
        }
    ]
}

...how can I write the 'stories' list of dictionaries to one csv file, such that the keys are the header row, and the values are all the rest of the rows. Note, that some keys don't appear in ALL of the records (example, some story dictionaries don't have a 'description' key, and some do).

Psuedo might include:

  1. Get all keys in the 'stories' list and assign those as the df's header
  2. Iterate through each story in the 'stories' list and append the appropriate rows, leaving a nan if there isn't a matching key for every column

Looking for a pythonic way of doing this relatively quickly.

UPDATE

Trying this:

# Save to excel file
with open("newsheadlines.csv", "wt") as fp:
    writer = csv.writer(fp, delimiter=",")
    # writer.writerow(["your", "header", "foo"])  # write header
    writer.writerows(response['stories'])

...gives this output

image

Does that help?

wildcat89
  • 1,159
  • 16
  • 47
  • Don't worry about pythonic first, worry about having any code at all and take it from there. Please share what you tried and where you're having problems and people will suggest fixes and improvements from there, pythonic or otherwise. People here like to [see you make an effort, but also want to be able to assess what you already understand and what kind of solution you're after](https://stackoverflow.com/help/how-to-ask) - just getting others to write your code is not how it works. – Grismar Jan 20 '22 at 02:09
  • Some ideas at [Reading and Writing CSV Files in Python](https://realpython.com/python-csv/), specifically [writing dict to CSV](https://realpython.com/python-csv/#writing-csv-file-from-a-dictionary-with-csv). – jarmod Jan 20 '22 at 02:10
  • Does this answer your question? [How do I read and write CSV files with Python?](https://stackoverflow.com/questions/41585078/how-do-i-read-and-write-csv-files-with-python) – Grismar Jan 20 '22 at 02:12
  • What is your _specific_ programming question? – John Gordon Jan 20 '22 at 02:16
  • @JohnGordon `...how can I write the 'stories' list of dictionaries to one csv file, such that the keys are the header row, and the values are all the rest of the rows. Note, that some keys don't appear in ALL of the records (example, some story dictionaries don't have a 'description' key, and some do).` – wildcat89 Jan 20 '22 at 02:18
  • Question has been updated above for anyone's review, and answered below. Thanks. – wildcat89 Jan 20 '22 at 02:28

1 Answers1

1

Simplest "pythonic" way to do so is by the pandas package.

import pandas as pd
pd.DataFrame(d["stories"]).to_csv('tmp.csv')

# To retrieve it
stories = pd.read_csv('tmp.csv', index_col=0)
Aaron Lei
  • 96
  • 4