Python 3.5: How to read a db of JSON objects - python

so I'm new to working with JSON and I'm trying to work with the openrecipe database from here. The db dump you get looks like this...
{ "_id" : { "$oid" : "5160756d96cc62079cc2db16" }, "name" : "Hot Roast Beef Sandwiches", "ingredients" : "12 whole Dinner Rolls Or Small Sandwich Buns (I Used Whole Wheat)\n1 pound Thinly Shaved Roast Beef Or Ham (or Both!)\n1 pound Cheese (Provolone, Swiss, Mozzarella, Even Cheez Whiz!)\n1/4 cup Mayonnaise\n3 Tablespoons Grated Onion (or 1 Tbsp Dried Onion Flakes))\n1 Tablespoon Poppy Seeds\n1 Tablespoon Spicy Mustard\n1 Tablespoon Horseradish Mayo Or Straight Prepared Horseradish\n Dash Of Worcestershire\n Optional Dressing Ingredients: Sriracha, Hot Sauce, Dried Onion Flakes Instead Of Fresh, Garlic Powder, Pepper, Etc.)", "url" : "http://thepioneerwoman.com/cooking/2013/03/hot-roast-beef-sandwiches/", "image" : "http://static.thepioneerwoman.com/cooking/files/2013/03/sandwiches.jpg", "ts" : { "$date" : 1365276013902 }, "cookTime" : "PT20M", "source" : "thepioneerwoman", "recipeYield" : "12", "datePublished" : "2013-03-13", "prepTime" : "PT20M", "description" : "When I was growing up, I participated in my Episcopal church's youth group, and I have lots of memories of weekly meetings wh..." }
{ "_id" : { "$oid" : "5160756f96cc6207a37ff777" }, "name" : "Morrocan Carrot and Chickpea Salad", "ingredients" : "Dressing:\n1 tablespoon cumin seeds\n1/3 cup / 80 ml extra virgin olive oil\n2 tablespoons fresh lemon juice\n1 tablespoon honey\n1/2 teaspoon fine sea salt, plus more to taste\n1/8 teaspoon cayenne pepper\n10 ounces carrots, shredded on a box grater or sliced whisper thin on a mandolin\n2 cups cooked chickpeas (or one 15- ounce can, drained and rinsed)\n2/3 cup / 100 g dried pluots, plums, or dates cut into chickpea-sized pieces\n1/3 cup / 30 g fresh mint, torn\nFor serving: lots of toasted almond slices, dried or fresh rose petals - all optional (but great additions!)", "url" : "http://www.101cookbooks.com/archives/moroccan-carrot-and-chickpea-salad-recipe.html", "image" : "http://www.101cookbooks.com/mt-static/images/food/moroccan_carrot_salad_recipe.jpg", "ts" : { "$date" : 1365276015332 }, "datePublished" : "2013-01-07", "source" : "101cookbooks", "prepTime" : "PT15M", "description" : "A beauty of a carrot salad - tricked out with chickpeas, chunks of dried pluots, sliced almonds, and a toasted cumin dressing. Thank you Diane Morgan." }
{ "_id" : { "$oid" : "5160757096cc62079cc2db17" }, "name" : "Mixed Berry Shortcake", "ingredients" : "Biscuits\n3 cups All-purpose Flour\n2 Tablespoons Baking Powder\n3 Tablespoons Sugar\n1/2 teaspoon Salt\n1-1/2 stick (3/4 Cup) Cold Butter, Cut Into Pieces\n1-1/4 cup Buttermilk\n1/2 teaspoon Almond Extract (optional)\n Berries\n2 pints Mixed Berries And/or Sliced Strawberries\n1/3 cup Sugar\n Zest And Juice Of 1 Small Orange\n SWEET YOGURT CREAM\n1 package (7 Ounces) Plain Greek Yogurt\n1 cup Cold Heavy Cream\n1/2 cup Sugar\n2 Tablespoons Brown Sugar", "url" : "http://thepioneerwoman.com/cooking/2013/03/mixed-berry-shortcake/", "image" : "http://static.thepioneerwoman.com/cooking/files/2013/03/shortcake.jpg", "ts" : { "$date" : 1365276016700 }, "cookTime" : "PT15M", "source" : "thepioneerwoman", "recipeYield" : "8", "datePublished" : "2013-03-18", "prepTime" : "PT15M", "description" : "It's Monday! It's a brand new week! The birds are chirping! The coffee's brewing! Everything has such hope and promise! A..." }
I tried the following code to read in the database
import json
f = r'<file_path>\recipeitems-latest.json'
with open(f) as dfile:
data = json.load(dfile)
print(data)
With this I received the following Traceback
Traceback (most recent call last):
File "C:/Users/<redacted>/Documents/<redacted>/project/test_json.py", line 7, in <module>
data = json.load(dfile)
File "C:\Users\<redacted>\AppData\Local\Continuum\Anaconda3\Lib\json\__init__.py", line 265, in load
return loads(fp.read(),
File "C:\Users\<redacted>\AppData\Local\Continuum\Anaconda3\Lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 101915: character maps to <undefined>
The only way I could find around this error was to only have one entry in the json file. Is the db formatted incorrectly or am I reading in the data wrong?
Thanks for any help!

The file is not a json array. Each line of the file is a json document, but the whole file is not in json format.
Read the file by lines, and use json.loads:
with open('some_file') as f:
for line in f:
doc = json.loads(line)
You may also need to pass the encoding parameter to open(). See here.

Related

Pass text to a Python script and return the result using R JSON

I have a string in R that I would like to pass to python in order to compute something and return the result back into R.
I have the following which "works" but not as I would like.
The below passes a string from R, to a Python file, uses openAI to collect the text data and then load it back into R.
library(reticulate)
computePythonFunction <- "
def print_openai_response():
import openai
openai.api_key = 'ej-powurjf___OpenAI_API_KEY___HGAJjswe' # you will need an API key
prompt = 'write me a poem about the sea'
response = openai.Completion.create(engine = 'text-davinci-003', prompt = prompt, max_tokens=1000)
#response['choices'][0]['text']
print(response)
"
py_run_string(computePythonFunction)
py$print_openai_response()
library("rjson")
fromJSON(as.character(py$print_openai_response()))
I would like to store the results in R objects - i.e. Here is one output from the python script.
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"text": "\n\nThe sea glitters like stars in the night \nWith hues, vibrant and bright\nThe waves flow gentle, serene, and divine \nLike the sun's most gentle shine\n\nAs the sea reaches, so wide, so vast \nAn adventure awaits, and a pleasure, not passed\nWhite sands, with seaweed green \nForms a kingdom of the sea\n\nConnecting different land and tide \nThe sea churns, dancing with the sun's pride\nAs a tempest forms, raging and wild \nThe sea turns, its colors so mild\n\nA raging storm, so wild and deep \nProtecting the creatures that no one can see \nThe sea is a living breathing soul \nA true and untouchable goal \n\nThe sea is a beauty that no one can describe \nAnd it's power, no one can deny \nAn ever-lasting bond, timeless and free \nThe love of the sea, is a love, to keep"
}
],
"created": 1670525403,
"id": "cmpl-6LGG3hDNzeTZ5VFbkyjwfkHH7rDkE",
"model": "text-davinci-003",
"object": "text_completion",
"usage": {
"completion_tokens": 210,
"prompt_tokens": 7,
"total_tokens": 217
}
}
I am interested in the text generated but I am also interested in the completion_tokens, promt_tokens and total_tokens.
I thought about save the Python code as a script, then pass the argument to it such as:
myPythin.py arg1.
How can I return the JSON output from the model to an R object? The only input which changes/varies in the python code is the prompt variable.

How to get fortnite stats in python

So i was trying to find something to code, and i decided to use python to get fortnite stats, i came across the fortnite_python library and it works, but it displays item codes for items in the shop when i want it to display the names. Anyone know how to convert them or just disply the name in the first place? This is my code.
​
fortnite = Fortnite('c954ed23-756d-4843-8f99-cfe850d2ed0c')
store = fortnite.store()
fortnite.store()
It outputs something like this
[<StoreItem 12511>,
To print out the attributes of a Python object you can use __dict__ e.g.
from fortnite_python import Fortnite
from json import dumps
fortnite = Fortnite('Your API Key')
# ninjas_account_id = fortnite.player('ninja')
# print(f'ninjas_account: {ninjas_account_id}') # ninjas_account: 4735ce91-3292-4caf-8a5b-17789b40f79c
store = fortnite.store()
example_store_item = store[0]
print(dumps(example_store_item.__dict__, indent=2))
Output:
{
"_data": {
"imageUrl": "https://trackercdn.com/legacycdn/fortnite/237112511_large.png",
"manifestId": 12511,
"name": "Dragacorn",
"rarity": "marvel",
"storeCategory": "BRSpecialFeatured",
"vBucks": 0
},
"id": 12511,
"image_url": "https://trackercdn.com/legacycdn/fortnite/237112511_large.png",
"name": "Dragacorn",
"rarity": "marvel",
"store_category": "BRSpecialFeatured",
"v_bucks": 0
}
So it looks like you want to use name attribute of StoreItem:
for store_item in store:
print(store_item.name)
Output:
Dragacorn
Hulk Smashers
Domino
Unstoppable Force
Scootin'
Captain America
Cable
Probability Dagger
Chimichanga!
Daywalker's Kata
Psi-blade
Snap
Psylocke
Psi-Rider
The Devil's Wings
Daredevil
Meaty Mallets
Silver Surfer
Dayflier
Silver Surfer's Surfboard
Ravenpool
Silver Surfer Pickaxe
Grand Salute
Cuddlepool
Blade
Daredevil's Billy Clubs
Mecha Team
Tricera Ops
Combo Cleaver
Mecha Team Leader
Dino
Triassic
Rex
Cap Kick
Skully
Gold Digger
Windmill Floss
Bold Stance
Jungle Scout
It seems that the library doesn't contain a function to get the names. Also this is what the class of a item from the store looks like:
class StoreItem(Domain):
"""Object containing store items attributes"""
and thats it.

Can not use .json information

Just a heads up I'm completely new to the coding scene and I'm having some issues using a json file
I've got the json to open using
json_queue = json.load(open('customer.json'))
but I just cant find the right code that allows me to make use of the info on the json. I think its because the json is an array not an object (probably completely wrong) My json currently looks like this
[
["James", "VW"],
["Katherine", "BMW"],
["Deborah", "renault"],
["Marguerite", "ford"],
["Kenneth", "VW"],
["Ronald", "Mercedes"],
["Donald", "BMW"],
["Al", "vauxhall"],
["Max", "porsche"],
["Carlos", "BMW"],
["Barry", "ford"],
["Donald", "renault"]
]
What I'm trying to do is take the persons name and the car type they are looking for and compare it too another json file that has the stock of cars in a shop but I'm currently stuck as to how I get python to actually use the information in that json.
I think I might of over explained my problem. My issue is that I am just starting a project using .json files and I can get python to open the file, but then I am unsure of how to get python to read that "James" wants a "VW" and then to go check the stock json to check if it is in stock. The stock json looks like this.
{
"VW": 4,
"BMW": 2,
"renault": 0,
"ford": 1,
"mercedes": 2,
"vauxhall": 1,
"porsche": 0,
}
What you have after the json.load() call is a plain python list of lists:
whishlist = [
["James", "VW"],
["Katherine", "BMW"],
["Deborah", "renault"],
["Marguerite", "ford"],
["Kenneth", "VW"],
["Ronald", "Mercedes"],
["Donald", "BMW"],
["Al", "vauxhall"],
["Max", "porsche"],
["Carlos", "BMW"],
["Barry", "ford"],
["Donald", "renault"]
]
where each sublist is a , pair. You can iterate over this list:
for name, car in whishlist:
print("name : {} - car : {}".format(name, car))
Now with your "other json file", what you have is a dict:
stock = {
"VW": 4,
"BMW": 2,
"renault": 0,
"ford": 1,
"mercedes": 2,
"vauxhall": 1,
"porsche": 0,
}
so all you have to do is to iterate over the whishlist list, check whether the car is in stock and print (or do anything else) the result:
for name, car in whishlist:
in_stock = stock.get(car, 0)
print("for {} : car {} in stock : {}".format(name, car, in_stock))
for James : car VW in stock : 4
for Katherine : car BMW in stock : 2
for Deborah : car renault in stock : 0
for Marguerite : car ford in stock : 1
for Kenneth : car VW in stock : 4
for Ronald : car Mercedes in stock : 0
for Donald : car BMW in stock : 2
for Al : car vauxhall in stock : 1
for Max : car porsche in stock : 0
for Carlos : car BMW in stock : 2
for Barry : car ford in stock : 1
for Donald : car renault in stock : 0

How do I fix this branching / story making on python?

sceneDict = {}
sceneDict["Beggining"] = {
"Branching" : False
"SceneText" : "Walking towards the enterance to the hollowed out tree you \
notice some large skulls on the sides of the dirt path along with other \
large sized bones which don't look like anything you've ever seen before.",
"NextScene" : "Enterance"
}
sceneDict["Enterance" = {
"Branching": True,
"SceneText" : "You come up to the cave enterance and upon inspecting it you notice some \
liquid dripping from the roof of the enterance, you catch a small amount in your hand.. It's \
red and has the consistency of blood. All of a sudden a thundering roar is heard behind you and a massive \
troll has appeared!",
"Choices" : [
{"ChoiceNumber" : "1. ",
"ChoiceText" : "Run into the cave to escape the troll",
"NextScene" : "Cave"},
{"ChoiceNumber" : "2. ",
"ChoiceText" : "Attempt to escape between the trolls legs",
"NextScene" : "Chase"},
{"ChoiceNumber" : "3. ",
"ChoiceText" : "Pull out your sword in an attempt to combat the troll"
"NextScene" : "Combat"}
]
}
sceneDict["Combat"] = {
"Branching": False,
"SceneText" : "As you reach back to grab your sword from your back the troll puts its arm out to the \
side and in one sweeping motion smashes you into a tree, unfortunately ending your life.",
"NextScene" : "Afterlife"
}
sceneDict["Afterlife"] = {
"Branching": True,
"SceneText" : "You feel sunlight on your face and as you open your eyes there is two pathways infront of you \
as well as a sign over top of both'that's strange, I should probably be dead' however, above the arrow signs \
there is a sign that reads 'You messed up, it happens but try again' the paths both lead to the two other choices\
you had before you took the worst of the three choices.",
"Choices" : [
{"ChoiceNumber" : "1. ",
"ChoiceText" : "Run into the cave to escape the troll",
"NextScene" : "Cave"},
{"ChoiceNumber" : "2. ",
"ChoiceText" : "Attempt to escape between the trolls legs",
"NextScene" : "Chase"}
]
}
sceneDict["Chase"] = {
"Branching": False,
"SceneText" : "You slide on your knees between the trolls legs as it's enormous fists come crashing down behind \
you, you get back up and start running down the hill towards your village hearing its thunderous steps close \
behind. The people of your town hear the ruckus and go towards the gate to see you being chased and they all \
start cheering you to keep going!",
"NextScene" : "Decisions"
}
sceneDict["Decisions"] = {
"Branching": True,
"SceneText" : "You are about 100 meters out from your village but you remember that there is a river to the \
right of the gate you could most likely lead the troll over there and have it crash through the bridge \
into the water, or you could chance the guards speed of closing the gate to block the troll.",
"Choices" : [
{"ChoiceNumber" : "1. ",
"ChoiceText" : "Continue towards the village",
"NextScene" : "Village"},
{"ChoiceNumber" : "2. ",
"ChoiceText" : "Run towards the water",
"NextScene" : "Bridge"}
]
}
sceneDict["Village"] = {
"Branching": False,
"SceneText" : "You run towards the village as fast as you possibly can the troll clearling gaining on you \
you call out for the guards to start closing the gate as you are running, the gate begins to close as you \
close in on it, you quickly slide through the last remaining opening as the gate slams shut followed by a loud \
'BANG!' as the troll smashes his head into the gate. The village cheers as you made it back alive!",
}
sceneDict["Bridge"] = {
"Branching": False,
"SceneText" : "As the villagers are cheering they start questioning what are you doing?! You change your path \
and go for the bridge. You start running over the wooden bridge as it is creeking and after a few seconds \
really shaking. Suddenly you hear a snap as the bottom gives out and you grab onto the side of the bridge. \
The bridge falls away at your feet only leaving the sides to hold onto as you hear a loud splash and see \
the troll floating away into the distance.",
}
sceneDict["Cave"] = {
"Branching": True,
"SceneText" : "You run forward into the cave, and slip on some of the blood into the depths of it. Behind you \
the troll is coming, however you hear the sound of splashing further into the cave.",
"Choices" : [
{"ChoiceNumber" : "1. ",
"ChoiceText" : "Run deeper into the cave towards the splashing",
"NextScene" : "Jump"},
{"ChoiceNumber" : "2. ",
"ChoiceText" : "Stand your ground to the troll",
"NextScene" : "Learn"}
]
}
sceneDict["Jump"] = {
"Branching": False,
"SceneText" : "You run towards the end of a cliff which has a small waterfall going over the edge of it \
you hear the troll gaining on you, but the drop is managable, you take a step back and leap for glory! You \
land with a splash and the troll sits at the top roaring in anger at your escape, you made it.. now to find \
your way home..",
}
sceneDict["Learn"] = {
"Branching": False,
"SceneText" : "You reach back to grab your sword from your back but in one swift movement the troll smashes you \
into the ground turning you into mince meat, very unfortunate.. You hear a shimmering sound and you appear back \
in on the path towards the cave again as if a God has given you a second chance or something.",
"NextScene" : "Enterance"
}
currentScene = "Beggining"
while currentScene != "":
sceneData = sceneDict[currentScene]
print(sceneData["SceneText"])
print()
if sceneData["Branching']:
for choice in sceneData["Choices"]:
print(choice["ChoiceNumber"] + choice["ChoiceText"])
print()
answer = input("> ")
print()
answer = int(answer) - 1
if answer <= len(sceneData["Choices"])
currentScene = sceneData["Choices"][answer]["NextScene"]
else:
currentScene = sceneData ["NextScene"]
window.exitonclick()
I have this code, and it should work it looks flawless, however I am getting a syntax error every time I run it but it does not point me to the error? Where is the error and how do I fix it if you could help that'd be great thanks!
In your while loop, you have used the incorrect closing quote in your if statement:
if sceneData["Branching']
You should use either:
if sceneData["Branching"]
or:
if sceneData['Branching']
Also, in order to have text on multiple lines you should do the following:
sceneDict["Beggining"] = {
"Branching" : False
"SceneText" : "Walking towards the enterance to the hollowed out tree you \n"
"notice some large skulls on the sides of the dirt path along with other \n"
"large sized bones which don't look like anything you've ever seen before.",
"NextScene" : "Enterance"
}

Populate dictionaries from text file

I have a text file with the details of a set of restaurants given one after the other. The details are name, rating, price and type of cuisines of a particular restaurant. The contents of text file is as given below.
George Porgie
87%
$$$
Canadian, Pub Food
Queen St. Cafe
82%
$
Malaysian, Thai
Dumpling R Us
71%
$
Chinese
Mexican Grill
85%
$$
Mexican
Deep Fried Everything
52%
$
Pub Food
I want to create a set of dictionaries as given below:
Restaurant name to rating:
# dict of {str : int}
name_to_rating = {'George Porgie' : 87,
'Queen St. Cafe' : 82,
'Dumpling R Us' : 71,
'Mexican Grill' : 85,
'Deep Fried Everything' : 52}
Price to list of restaurant names:
# dict of {str : list of str }
price_to_names = {'$' : ['Queen St. Cafe', 'Dumpling R Us', 'Deep Fried Everything'],
'$$' : ['Mexican Grill'],
'$$$' : ['George Porgie'],
'$$$$' : [ ]}
Cuisine to list of restaurant name:
#dic of {str : list of str }
cuisine_to_names = {'Canadian' : ['George Porgie'],
'Pub Food' : ['George Porgie', 'Deep Fried Everything'],
'Malaysian' : ['Queen St. Cafe'],
'Thai' : ['Queen St. Cafe'],
'Chinese' : ['Dumpling R Us'],
'Mexican' : ['Mexican Grill']}
What is the best way in Python to populate the above dictionaries ?
Initialise some containers:
name_to_rating = {}
price_to_names = collections.defaultdict(list)
cuisine_to_names = collections.defaultdict(list)
Read your file into a temporary string:
with open('/path/to/your/file.txt') as f:
spam = f.read().strip()
Assuming the structure is consistent (i.e. chunks of 4 lines separated by double newlines), iterate through the chunks and populate your containers:
restraunts = [chunk.split('\n') for chunk in spam.split('\n\n')]
for name, rating, price, cuisines in restraunts:
name_to_rating[name] = rating
# etc ..
for the main reading loop, you can use enumerate and modulo to know what is the data on a line:
for lineNb, line in enumerate(data.splitlines()):
print lineNb, lineNb%4, line
for the price_to_names and cuisine_to_names dictionnaries, you could use a defaultdict:
from collections import defaultdict
price_to_names = defaultdict(list)

Categories

Resources