I have to use boto because not all employees have access to CLI or they do not know how to use CLI. It seems like boto is a guess game as I do not see the result of API call that I make with boto. Following is an example,
groups=autoscale_connection.get_all_groups()
print groups
using AWS CLI you can get output in a JSON file that you can parse easily
However it would be great if we can store output in a json file and then I can look at it and operate on the data I have in file.
This didn't work?
from boto.ec2.autoscale import AutoScaleConnection
conn = AutoScaleConnection()
git_em = conn.get_all_groups()
print git_em
If you have your .boto and other config files set to be json, it should pop right out.
In python,the dictionary data structure is equivalent to a JSON object.
If there exits, you should get a list of AG's as per your code(Note: get_all_groups returns a list). Then you just need to convert the list to dict, as per your requirement.
Say to create a map like:
AG(keys) -> Description(value) It's easy in python if you explore.
Related
I am new to JSON, never used python packages to manipulate JSON files. I have 10 JSON files that I would like to merge into one using python.
Each of 10 files has the exact same structure and has about 50,000 entries
Example:
File one
{"tracking_code":"21703238","from_country":"FR","to_country":"FR","amount":3.23}
...
Example: File two
{"tracking_code":"41545695","from_country":"FR","to_country":"FR","amount":2.9}
...
Desired output would simply be:
{"tracking_code":"21703238","from_country":"FR","to_country":"FR","amount":3.23}
{"tracking_code":"41545695","from_country":"FR","to_country":"FR","amount":2.9}
The second part of my questions would be this - how would I join JSON files based on one key? I would like to join these 2 files by "tracking_code", the output file would simply add '"amount":3.23' to the first file.
Example: File one:
{"tracking_code":"29285908","from_country":"FR","to_country":"FR",
"package_type_id":10,"transaction_id":172238850,
"shipping_label_created":"2018-09-25 18:40:52"}
Example: File two
{"tracking_code":"29285908","from_country":"FR","to_country":"FR","amount":3.23}
Desired output:
{"tracking_code":"29285908","from_country":"FR","to_country":"FR",
"package_type_id":10,"transaction_id":172238850,
"shipping_label_created":"2018-09-25 18:40:52","amount":3.23}
Thank you.
If you use json.loads() (that "converts" json to python dictionary), you can merge them using a similar function :
def dict_merge(dict1, dict2):
return(dict2.update(dict1))
and then use json.dumps() to serialize the resulting dictionary to json.
Other solution :
you can also use json-merger (installation via pip install json-merger)
I have a JSON file that looks like this:
I have a list of device ID's, and I'd like to search my JSON for a specific value of the id, to get the name.
The data that is now is JSON format used to be in XML format, for which I used to do this:
device = xml.find("devices/device[#id=\'%s\']" %someDeviceID)
deviceName = device.attrib['name']
--
So far based on answers online I have managed to search the JSON for a jey, but I haven't yet managed to search for a value.
Personally to read a json file I use the jsondatabase module. Using this module I would use the following code
from jsondb.db import Database
db = Database('PATH/TO/YOUR/JSON/FILE')
for device in db['devices']:
if device['id'] == 'SEARCHEDID':
print(device['name'])
Of course when your json is online you could scrape it with the requests module and then parse it to the jsondatabase module
I have a code for automating web application( i am using Python and Selenium ),where i am entering the static data, i want to use JSON file to send the data to the application, can anyone please help me to how to write the code to pick from JSON file. Here is my code :-
import unittest
from selenium import webdriver
name = driver.find_element_by_xpath("some xpath").send_keys("xxxx")
pass = driver.find_element_by_xpath("some xpath").send_keys("xxxx")
phone_no = driver.find_element_by_xpath("some xpath").send_keys("xxxx")
Please help me in how to read data from json file.
Not sure if this is what you're looking for but, the simplest to read json is using the json module. json.load deserializes the json into a python object, usually a dictionary.
import json
with open('file-name.json') as data_file:
data = json.load(data_file)
# access json (now python object) like this... data['some-field']
When using the send_keys function, you can't just send the json as is, because it will add '\n' after every json attribute. Here are three ways to do it:
Way 1 - Do not use as a json, just use plain text
path = os.path.abspath("../excel_upload.json")
with open(path, "r") as fp:
obj = fp.readlines()
print(str(obj))
driver.find_element_by_name("name").send_keys((obj))
Way 2 - Serialization of the json, which is in the form of Dict, using json.dumps.
Way 3 - Loading the json and then removing '\n' by taking it in a string.
I used the first way for taking the complete json text, while second way to use key pairs. There will be a better way, please update with better solutions.
Loading and reading json is fine, but using it in send_keys is a different issue.
I am using tkinter to manage the GUI for a note retrieval program. I can pull my notes by typing a key word and hitting Enter in a text field but I would like to move my dictionary to a file so that my code space is not filled up with a massive dictionary.
I have been looking around but I am not sure how I would go about doing this.
I have the file in my directory. I know I can use open(“filename”, “mode”) to open said file for reading but how do I call each section of the notes.
For example what I do now is just call a keyword from my dictionary and have it write the definition for that keyword to a text box in my GUI. Can I do the same from the file?
How would I go about reading from the file the keyword and returning the definition to a variable or directly to the text box? For now I just need to figure out how to read the data. I think once I know that I can figure out how to write new notes or edit existing notes.
This is how I am set up now.
To call my my function
root.bind('<Return>', kw_entry)
How I return my definition to my text box
def kw_entry(event=None):
e1Current = keywordEntry.get().lower()
if e1Current in notes:
root.text.delete(1.0, END)
root.text.insert(tkinter.END, notes[e1Current])
root.text.see(tkinter.END)
else:
root.text.delete(1.0, END)
root.text.insert(tkinter.END, "Not a Keyword")
root.text.see(tkinter.END)
Sound's like you'd need to load the dictionary to memory at init time, and use it like a normal dictionary.
I am assuming your dictionary is a standard python dict of strings, so I recommend using the python json lib.
Easiest way to do this is to export the dictionary as json once to a file using something like:
with open(filename, 'w') as fp:
json.dump(dictionary, fp)
and then change your code to load the dict at init time using:
with open(filename) as fp:
dictionary = json.load(fp)
Alternatively, if your data is more complex than text, you can use python shelve which is a persistent, dictionary-like object to which you can pass any pickle-able object. Note that shelve has its drawbacks so read the attached doc.
sqlitedict is a project providing a persistent dictionary using sqlite in the background. You can use it like a normal dictionary e.g. by assigning arbitrary (picklable) objects to it.
If you access an element from the dictionary, only the value you requested is loaded from disk.
Hello I have combed threw possible solutions and I can't figure this out, I got my program working for the most part but the problem is when I close the program it doesn't remember the changes made by the user. The program stored input into variables. It works like a dictionary (I wrote the code before I knew about the dictionary function) when I open the program I can enter the input but the problem is when I close the program it forgets all the changes and goes back to how it was when first compiled. Any ideas how I can allow the data to ramain available when I close the program and reopen it?
Probably you need shelve. It is awesome Python library, which allows you to read/write dict-like data from file. For example:
import shelve
dict_with_data = dict(key1='value1', key2='value2')
storage = shelve.open('shelve_file')
storage.update(dict_with_data)
storage.close()
It is very simple example which opens shelve storage and updates it with your data. Actually you can do whatever you do with regular dictionary object, because
A “shelf” is a persistent, dictionary-like object.
Another library that you could use is pickle. It allows you serialize every kind of Python objects, but since you have dictionary structure shelve should be better for your needs.
Please fill free to ask questions/post your code, I will be glad to update my answer appropriate to you needs.
I think you need to implement two things here.
1) Your program need to take an optional input file and the best way to do it is probably to use argparse. If you pass the input file, then you extract the data from the file and you initialize your dictionary_like object with that data.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--input', help='input file name')
args = parser.parse_args()
if args.input:
... # open the file, load the data, initialize a dict with the data
2) Just before quitting, you must save your data (key:value pairs) in the input file. If you want to save a dictionary like structure, a txt file with a key:value pairs for each line of the file should be good enough.
with open(input, 'w') as f:
for key,value in dictionary.items():
f.write(str(key)+':'+str(value)+'\n') #you can use '\t' or whatever instead the ':'
And that's it.
If you need more help on how to implement this let me know ;)