How to read and write dictionaries to external files in python? [duplicate] - python
This question already has answers here:
Why can't Python parse this JSON data? [closed]
(3 answers)
Closed 5 years ago.
I have a dictionary in python. I want to modify that dictionary and then save the dictionary to an external file so that when I load up the python program again it grabs the dictionary data from the external file.
class Data:
"""
Data handling class to save
and receive json data, parent
of User for data purposes.
"""
def saveData(data, file):
with open(file, 'r+') as dataFile:
dataFile.write(json.dumps(data))
def getData(file):
with open(file, 'r+') as dataFile:
return json.loads(dataFile.readline())
def deleteContent(file):
file.seek(0)
file.truncate()
But when I write to the file and then try to read it reads it as a string and I can't use the read data to set a dictionary. How can I get data in a dictionary from an external JSON file as dictionary data, not string data?
data = Data.getData("chatbotData.json")
dataDict = data
dataDict["age"] = 2
Here is what I want to do with the data and I get this error:
TypeError: 'str' object does not support item assignment
Let's create a dictionary:
>>> d = {'guitar':'Jerry', 'drums':'Mickey' }
Now, let's dump it to a file:
>>> import json
>>> json.dump(d, open('1.json', 'w'))
Now, let's read it back in:
>>> json.load(open('1.json', 'r'))
{'guitar': 'Jerry', 'drums': 'Mickey'}
Taking better care of file handles
The above illustrates the json module but was sloppy about closing files. Better:
>>> with open('1.json', 'w') as f:
... json.dump(d, f)
...
>>> with open('1.json') as f:
... json.load(f)
...
{'guitar': 'Jerry', 'drums': 'Mickey'}
Related
How to read data from a CSV into a tuple in Python? [duplicate]
This question already has answers here: Reading a CSV file using Python (2 answers) Closed 3 years ago. I'm attempting to load a CSV which looks like the following and create a tuple out of it, so that I can later access account1, account2, etc in code: user,password johndoe#gmail.com,password janesmith#gmail.com,password2 So far, all I've been able to do is import the CSV. import csv # our csv file name filename = "resources/accounts.csv" with open(filename) as infile: reader = csv.reader(infile) I'm looking for something like so: {("johndoe#gmail.com", "password"), ("janesmith#gmail.com", "password")} Could someone advise on what to do from here?
import csv # our csv file name filename = "resources/accounts.csv" with open(filename) as infile: reader = csv.reader(infile) accounts = {"accounts" : []} for line in reader: accounts["accounts"].append({"user":line[0], "pass": line[1]}) print(accounts["accounts"][1]["user"]) Sorry I did this on mobile hope it shows up
Reading a Json in python [duplicate]
This question already has answers here: Reading JSON from a file [duplicate] (7 answers) Closed 4 years ago. I need to get a particular key:value from a json file. Code used to open json file : import json data = repr(open('file.json', 'rb').read()) print(data) Output( Json file) : b'b\'{"image":{"width":750,"height":1127,"orientation":1},"objects":[{"type":"face","boundingBox":{"x":132,"y":180,"height":619,"width":513},"landmarks":{"faceContour":[[162,375],[162,440],[165,504],[175,568],[193,628],[226,686],[265,740],[313,784],[373,799],[436,793],[494,758],[543,712],[586,660],[616,601],[631,534],[639,468],[645,400]],"noseBridge":[[387,396],[385,441],[381,485],[378,532]],"noseBall":[[322,547],[350,560],[380,572],[412,563],[442,554]],"eyebrowRight":[[419,348],[466,329],[516,325],[564,339],[595,377]],"eyebrowLeft":[[197,358],[224,325],[269,315],[316,325],[358,347]],"eyeRight":[[448,419],[479,404],[510,405],[539,416],[511,425],[480,424]],"eyeRightCenter":[[495,416]],"eyeLeft":[[242,402],[269,393],[299,396],[329,412],[297,414],[267,413]],"eyeLeftCenter":[[284,405]],"mouthOuter":[[269,596],[310,598],[349,598],[379,606],[411,601],[456,606],[502,609],[456,663],[412,684],[378,687],[345,680],[307,652]],"mouthInner":[[284,603],[349,610],[379,615],[411,613],[487,614],[412,652],[379,656],[348,648]]},"attributes":{"gender":"male","genderConfidence":0.8385,"age":24,"ageConfidence":0.8419,"emotion":"happiness","emotionConfidence":1.0,"emotionsAll":{"neutral":0.0,"sadness":0.0,"disgust":0.0,"anger":0.0,"surprise":0.0,"fear":0.0,"happiness":1.0},"pose":{"pitch":-5.7105,"roll":0.2941,"yaw":1.8646},"race":{"asian":0.0002,"black":0.0001,"white":0.9989},"eyewear":{"sunglasses":0.0,"glasses":0.0},"hair":{"color":{"blond":0.0,"black":0.9994,"brown":0.0},"cut":{"short":0.9999,"long":0.0,"bald":0.0},"facial":{"beard":0.0,"mustache":0.0,"stubble":0.2543}},"frontal":true}},{"type":"person","boundingBox":{"x":20,"y":60,"height":1053,"width":696}}],"requestId":"1239be0c782440bd828cee21dbc2baa1"}\'\r\n' From this Json file, I need to extract a particular key:value. For example, I need to get the emotionsAll (Key) from the file. I tried using : print(data['emotionsAll'][0]['neutral']) error i got : Traceback (most recent call last): File "C:\Users\HP\Downloads\Final Project\getfile.py", line 6, in <module> print(data['emotionsAll'][0]['neutral']) TypeError: string indices must be integers So, Is there any other way to get a particular key:value ?
Try using the json package Contents of file.json {"image":{"width":750,"height":1127,"orientation":1},"objects":[{"type":"face","boundingBox":{"x":132,"y":180,"height":619,"width":513},"landmarks":{"faceContour":[[162,375],[162,440],[165,504],[175,568],[193,628],[226,686],[265,740],[313,784],[373,799],[436,793],[494,758],[543,712],[586,660],[616,601],[631,534],[639,468],[645,400]],"noseBridge":[[387,396],[385,441],[381,485],[378,532]],"noseBall":[[322,547],[350,560],[380,572],[412,563],[442,554]],"eyebrowRight":[[419,348],[466,329],[516,325],[564,339],[595,377]],"eyebrowLeft":[[197,358],[224,325],[269,315],[316,325],[358,347]],"eyeRight":[[448,419],[479,404],[510,405],[539,416],[511,425],[480,424]],"eyeRightCenter":[[495,416]],"eyeLeft":[[242,402],[269,393],[299,396],[329,412],[297,414],[267,413]],"eyeLeftCenter":[[284,405]],"mouthOuter":[[269,596],[310,598],[349,598],[379,606],[411,601],[456,606],[502,609],[456,663],[412,684],[378,687],[345,680],[307,652]],"mouthInner":[[284,603],[349,610],[379,615],[411,613],[487,614],[412,652],[379,656],[348,648]]},"attributes":{"gender":"male","genderConfidence":0.8385,"age":24,"ageConfidence":0.8419,"emotion":"happiness","emotionConfidence":1.0,"emotionsAll":{"neutral":0.0,"sadness":0.0,"disgust":0.0,"anger":0.0,"surprise":0.0,"fear":0.0,"happiness":1.0},"pose":{"pitch":-5.7105,"roll":0.2941,"yaw":1.8646},"race":{"asian":0.0002,"black":0.0001,"white":0.9989},"eyewear":{"sunglasses":0.0,"glasses":0.0},"hair":{"color":{"blond":0.0,"black":0.9994,"brown":0.0},"cut":{"short":0.9999,"long":0.0,"bald":0.0},"facial":{"beard":0.0,"mustache":0.0,"stubble":0.2543}},"frontal":true}},{"type":"person","boundingBox":{"x":20,"y":60,"height":1053,"width":696}}],"requestId":"1239be0c782440bd828cee21dbc2baa1"} Code: import json with open('file.json', 'rb') as fp: json_data = json.load(fp) print(json_data['objects'][0]['attributes']['emotionsAll']['neutral'])
A couple things: 1 - the emotionsAll key is within the objects key, first element in the list [0], attributes key 2 - Your json file was written with the bytes prefix, so when it's being read, it starts your string with b'. You can either a) have that file written without that mode by decoding/encoding, or just manipulate that string. import json data = repr(open('file.json', 'rb').read()) data = data.split('{', 1)[-1] data = data.rsplit('}', 1)[0] data = ''.join(['{', data, '}']) jsonObj = json.loads(data) print(jsonObj['objects'][0]['attributes']['emotionsAll']['neutral']) Output: print(jsonObj['objects'][0]['attributes']['emotionsAll']['neutral']) 0.0
How to write in text file by variable using python 2.7 [duplicate]
This question already has answers here: How do I append to a file? (13 answers) Closed 5 years ago. I have stored the string in a variable. And I want to append it to a text file. How can I achieve this? def readyaml(abs_read_path,): with open(abs_read_path, 'r') as stream, open("instanceinput.txt",'w') as fnew: try: content = yaml.load(stream) instance = content['instance'] dump = instance[0] print dump fnew.write(dump) except yaml.YAMLError as exc: print(exc) stream.close() fnew.close() readyaml(abs_read_path)
You need use the append method as Vikas & Mayur has mentioned and also when writing to a file convert it to a sting object: Example: def readyaml(abs_read_path,): with open(abs_read_path, 'r') as stream, open("instanceinput.txt",'a') as fnew: try: content = yaml.load(stream) instance = content['instance'] dump = instance[0] print dump fnew.write(str(dump)) # CONVERT TO STRING OBJECT except yaml.YAMLError as exc: print(exc) stream.close() fnew.close() readyaml(abs_read_path)
use a instead of w : with open(abs_read_path, 'r') as stream, open("instanceinput.txt",'a') as fnew:
You can use 'a' or 'a+' instead of 'w' 'a' The file is created if it does not exist. Open for writing and appends the data. 'a+'The file is created if it does not exist. Open for both reading and writing and appends the data while writing.
how to clean a JSON file and store it to another file in Python
I am trying to read a JSON file with Python. This file is described by the authors as not strict JSON. In order to convert it to strict JSON, they suggest this approach: import json def parse(path): g = gzip.open(path, 'r') for l in g: yield json.dumps(eval(l)) however, not being familiar with Python, I am able to execute the script but I am not able to produce any output file with the new clean JSON. How should I modify the script in order to produce a new JSON file? I have tried this: import json class Amazon(): def parse(self, inpath, outpath): g = open(inpath, 'r') out = open(outpath, 'w') for l in g: yield json.dumps(eval(l), out) amazon = Amazon() amazon.parse("original.json", "cleaned.json") but the output is an empty file. Any help more than welcome
import json class Amazon(): def parse(self, inpath, outpath): g = open(inpath, 'r') with open(outpath, 'w') as fout: for l in g: fout.write(json.dumps(eval(l))) amazon = Amazon() amazon.parse("original.json", "cleaned.json")
another shorter way of doing this import json class Amazon(): def parse(readpath, writepath): with open(readpath) as g, open(writepath, 'w') as fout: for l in g: json.dump(eval(l), fout) amazon = Amazon() amazon.parse("original.json", "cleaned.json") While handling json data it is better to use json modules json.dump(json, output_file) for dumping json in file and json.load(file_path) to load the data. In this way you can get maintain json wile saving and reading json data. For very large amount of data say 1k+ use python pandas module.
Write Python dictionary obtained from JSON in a file
I have this script which abstract the json objects from the webpage. The json objects are converted into dictionary. Now I need to write those dictionaries in a file. Here's my code: #!/usr/bin/python import requests r = requests.get('https://github.com/timeline.json') for item in r.json or []: print item['repository']['name'] There are ten lines in a file. I need to write the dictionary in that file which consist of ten lines..How do I do that? Thanks.
To address the original question, something like: with open("pathtomyfile", "w") as f: for item in r.json or []: try: f.write(item['repository']['name'] + "\n") except KeyError: # you might have to adjust what you are writing accordingly pass # or sth .. note that not every item will be a repository, there are also gist events (etc?). Better, would be to just save the json to file. #!/usr/bin/python import json import requests r = requests.get('https://github.com/timeline.json') with open("yourfilepath.json", "w") as f: f.write(json.dumps(r.json)) then, you can open it: with open("yourfilepath.json", "r") as f: obj = json.loads(f.read())