Accessing dictionaries with field names - python

I have an unpickle function which returns a dict as:
def unpickle(file):
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
and a function which reads pickled object with fieldnames (Don't know if this is the correct definiton):
def do_sth():
all_data = unpickle('mypickle.pickle')
image_filenames = all_data["Filenames"]
conditions = all_data["Labels"]
I have two lists as Filenames = ['001.png','002.png'] and Labels = ['0','1'] for brevity, that I need to pickle and save under mypickle.pickle so I can call them under the do_sth function. Till now what I did is:
data = [Filenames,Labels]
with open("mypickle.pickle", "wb") as f:
pickle.dump(data, f)
and
data = dict(zip(file_paths, labels))
with open("mypickle.pickle", "wb") as f:
pickle.dump(data, f)
But I'm getting KeyError :'Filenames'. Which structure shall I use to save these 2 lists so they may work properly.
Thanks.

Change your function to this
def do_sth():
all_data = unpickle('mypickle.pickle')
image_filenames = all_data[0]
conditions = all_data[1]
Explanation
You saved pickle as list. When you load the pickle it is still a list.
or
Actually save it as a dict
data = {"Filenames": Filenames, "Labels": Labels}
with open("mypickle.pickle", "wb") as f:
pickle.dump(data, f)

Related

How can I edit my code to print out the content of my created json file?

My program takes a csv file as input and writes it as an output file in json format. On the final line, I use the print command to output the contents of the json format file to the screen. However, it does not print out the json file contents and I don't understand why.
Here is my code that I have so far:
import csv
import json
def jsonformat(infile,outfile):
contents = {}
csvfile = open(infile, 'r')
reader = csvfile.read()
for m in reader:
key = m['No']
contents[key] = m
jsonfile = open(outfile, 'w')
jsonfile.write(json.dumps(contents))
csvfile.close()
jsonfile.close()
return jsonfile
infile = 'orders.csv'
outfile = 'orders.json'
output = jsonformat(infile,outfile)
print(output)
Your function returns the jsonfile variable, which is a file.
Try adding this:
jsonfile.close()
with open(outfile, 'r') as file:
return file.read()
Your function returns a file handle to the file jsonfile that you then print. Instead, return the contents that you wrote to that file. Since you opened the file in w mode, any previous contents are removed before writing the new contents, so the contents of your file are going to be whatever you just wrote to it.
In your function, do:
def jsonformat(infile,outfile):
...
# Instead of this:
# jsonfile.write(json.dumps(contents))
# do this:
json_contents = json.dumps(contents, indent=4) # indent=4 to pretty-print
jsonfile.write(json_contents)
...
return json_contents
Aside from that, you aren't reading the CSV file the correct way. If your file has a header, you can use csv.DictReader to read each row as a dictionary. Then, you'll be able to use for m in reader: key = m['No']. Change reader = csvfile.read() to reader = csv.DictReader(csvfile)
As of now, reader is a string that contains all the contents of your file. for m in reader makes m each character in this string, and you cannot access the "No" key on a character.
a_file = open("sample.json", "r")
a_json = json.load(a_file)
pretty_json = json.dumps(a_json, indent=4)
a_file.close()
print(pretty_json)
Using this sample to print the contents of your json file. Have a good day.

How to open multiple Json files and save them in multiple variables

I have some Json files. The naming convention of the file is dataset_ML-Model_params.json. For example, House_Lasso_params.json,
House_RF_params.json, Bike_Lasso_params.json, and Bike_RF_params.json.
All of these files contains tuning-hyperparameters in dict format. I can open 1 file using the below code
filename = f"{args.dataset}_Lasso_params.json"
outfile = HT_OUT / filename
with open(outfile, "r") as file:
d_loaded = json.load(file)
Passing the value to the model.
Lasso(**d_loaded, precompute=True)
Again for another file
filename = f"{args.dataset}_RF_params.json"
outfile = HT_OUT / filename
with open(outfile, "r") as file:
rf_loaded = json.load(file)
RF(**rf_loaded)
Here, args.dataset contains the dataset name. Could you tell me, how can I load these 2 files and save them in different variables. So that later i can pass the variable to the model. Like
# After opening and saving the json file in different variable
Lasso(**lasso_params, precompute=True)
RF(**rf_params)
Make a list of all models
MODEL_NAMES = ["Lasso", "Ridge"]
Make another dictionary to save the params for each model
models_params = {}
for model_name in MODEL_NAMES:
filename = f"{args.dataset}_{model_name}_params.json"
outfile = HT_OUT / filename
with open(outfile, "r") as file:
d_loaded = json.load(file)
models_params[model_name] = d_loaded
Later, use the get(key) to access your expected params.
Lasso(**(models_params.get('Lasso')), precompute=True)
RF(**(models_params.get('RF')))
You can also check the params
print(Lasso(**(models_params.get('Lasso')), precompute=True).get_params())
You could use another dict that gonna contain params that you need.
For example,
model_params = {'lasso_params': smth_here, 'rf_params': smth_here}
So then you can get needed value by
*model_params['lasso_params']
To get all files by that wildcard (dataset_ML-Model_params.json.) you could use library called glob:
from glob import glob
glob('*_params.json') # return ['lasso_params', 'rf_params', ...]
And then just read them one by one.

Combine two geojson state zipcode files?

I am working on a project where I need to use US States Zip Code Data. I want to merge two geojson files while preserving the data in those files. geojson-merge https://github.com/mapbox/geojson-merge does this but I am hoping for a python based solution.
Each state has a separate *.json file. For example:
mt_montana_zip_codes_geo.min.json
nd_north_dakota_zip_codes_geo.min.json
import json
nd_boundary_file = r"C:\Data_ZipCodes_States\State-zip-code-GeoJSON-master" \
r"\nd_north_dakota_zip_codes_geo.min.json"
with open(nd_boundary_file, 'r') as f:
nd_zipcode_boundary = json.load(f)
mt_boundary_file = r"C:\\Data_ZipCodes_States\State-zip-code-GeoJSON-master" \
r"\mt_montana_zip_codes_geo.min.json"
with open(mt_boundary_file, 'r') as f:
mt_zipcode_boundary = json.load(f)
#This overwrote the mt_zipcode_boundary with the nd_zipcode_boundary into merged
#merged = {**mt_zipcode_boundary, **nd_zipcode_boundary}
#produced a file with two json objects one 'mt' and the other 'nd'
data = {'mt': mt_zipcode_boundary, 'nd':nd_zipcode_boundary}
#Also overwrote mt_zipcode_boundary
mt_zipcode_boundary.update(nd_zipcode_boundary)
How would I write code to combine these two geojson files into a single file?
What about something like this?
import json
fc = {
'type': 'FeatureCollection',
'features': []
}
with open("mt_montana_zip_codes_geo.min.json") as json_file:
obj = json.load(json_file)
fc['features'].extend(obj['features'])
with open("nd_north_dakota_zip_codes_geo.min.json") as json_file:
obj = json.load(json_file)
fc['features'].extend(obj['features'])
with open("merged.json", "w") as outfile:
json.dump(fc, outfile)

Save data in JSON file

I am missing something in below code, not able to save in correct format, Can you please guide where I'm going wrong...
Python Code
str_next_thursday_expiry = 23JUL2020
f = open("data/expiry.json","r")
with open("data/expiry.json", "w") as f:
json.dump(str_next_thursday_expiry, f)
Output in expiy.json
"23JUL2020"
I want to store this in below format, not getting what needs to be corrected..
{"expirydate": "23JUL2020"}
str_next_thursday_expiry = "23JUL2020"
with open("data/expiry.json", "w") as f:
data = {"expirydate":str_next_thursday_expiry}
json.dump(data, f)
try this
content = {"expirydate": "23JUL2020"}
with open("data/expiry.json", "w") as f:
json.dump(str_next_thursday_expiry, f)

Save/load function in Python

I have to create a save function and a load function that saves a dictionary in the format of:
123;Kalle;
123;Maria;
321;Anna;
321;Olle;
My dictionary is supposed to look like a phonebook, with the key being the name and the value is the phonenumber:
telebook = {"jacob":"8472923777", "nisse":"092563243"}
How can I write a function that saves my phonebook in the format mentioned? It should look like this:
8472923777;jacob;
This is my current code:
def save(lista, telebook):
import pickle
filename = lista[1]
f = open(filename, "w")
pickle.dump(telebook, f)
f.close()
print telebook
def load(lista, telebook):
import pickle
try:
filename = lista[1]
f = open(filename, "r")
telebook_1 = pickle.load( f )
telebook.clear()
telebook.update(telebook_1)
f.close()
print telebook
except:
print "This file doesn't exist"
EDIT:
My save function was easier than I thought, managed to solve it on my own. Not sure how to get the load function to work though.
book = raw_input("telebook> ").lower()
lista = book.split()
def save(lista, telebook):
filename = lista[1]
f = open(filename, "w")
for name, num in telebook.items():
f.write(num+";"+name+";"+"\n")
f.close()
print telebook
My load is the same as before but obviously I can't use that one anymore.
def save(telebok, filepath):
with open(filepath, 'w') as outfile:
for name,num in telebok.items():
outfile.write("{};{};\n".format(num, name))
And to get it back:
import csv
def load(filepath):
with open(filepath) as infile:
telebok = dict((v,k) for v,k,_ in csv.reader(infile, delimiter=';'))
return telebok

Categories