Python Forum
How to save dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to save dictionary (/thread-26195.html)



How to save dictionary - sampitosh - Apr-23-2020

I wrote a code to load json contents. How should I modify this to save it as a dictionary or even save it as a data frame?

import json
import traceback
import pandas as pd

def read_json_file(fname):
    with open(fname, "r") as f:
        try:
            return json.load(f)
        except Exception as e:
            print ("Error parsing %s" %fname)
            traceback.print_exc()
            raise e
print(read_json_file("/Desktop/file1.txt"))



RE: How to save dictionary - ndc85430 - Apr-23-2020

What do you mean "save"? json.load will already give you a Python data structure that maps to JSON types (so a dictionary if you have a JSON object). More details please!

Also, use "[python]" tags for code, not "[quote]".


RE: How to save dictionary - sampitosh - Apr-23-2020

I was looking to save this dictionary as an object "d" or as a dataframe variable "df". My question is how can i do that from this code?


RE: How to save dictionary - ndc85430 - Apr-24-2020

Again, what do you mean by "save"? Do you just want to assign to a variable? If so, what's stopping you assigning the value returned from your function?