Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to save dictionary
#1
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"))
Reply
#2
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]".
Reply
#3
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?
Reply
#4
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 509 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  Simple Dictionary save and recover Astrikor 12 5,852 Aug-22-2018, 06:51 AM
Last Post: Astrikor

Forum Jump:

User Panel Messages

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