python: print dictionary keys to a file - python

I'd like to figure out how to take all dictionary keys from an API call, and insert them into a flat file.
#!/usr/bin/env python
import requests
import json
import time
import urllib3
from base64 import b64encode
requests.packages.urllib3.disable_warnings()
#
# GET /dashboards/{dashboardId}/widgets/{widgetId}/value
test_dashboard = "557750bee4b0033aa111a762"
test_widget = "8bad2fc0-5c9b-44f2-a54b-05c8c6f9552b"
apiserver = "http://serveraddress"
userpass = b64encode(b"myuser:mypass").decode("ascii")
headers = { 'Authorization' : 'Basic %s' % userpass }
def get_apicall(dashboardId, widgetId):
response = requests.get(
apiserver + "/dashboards/" +
dashboardId + "/widgets/" +
widgetId + "/value",
headers=headers,
verify=False)
json_data = json.loads(response.text)
print(json.dumps(json_data["result"]["terms"], indent=2))
get_apicall(test_dashboard, test_widget)
which outputs something like:
[user#host ]$ ./shunhosts.py
{
"71.6.216.39": 2,
"71.6.158.166": 2,
"71.6.216.55": 2,
"71.6.216.56": 2
}
I would like the code to write/append each dictionary key to new line in a flat text file: i.e.
71.6.216.39
71.6.158.166
71.6.216.55
71.6.216.56

If you have a dictionary as
d = {
"71.6.216.39": 2,
"71.6.158.166": 2,
"71.6.216.55": 2,
"71.6.216.56": 2
}
You can get your keys with keys():
d.keys()
dict_keys(['71.6.216.56', '71.6.216.39', '71.6.158.166', '71.6.216.55'])
Make it to a string that is new-line separated:
s = '\n'.join(d.keys())
print(s)
71.6.216.39
71.6.158.166
71.6.216.55
71.6.216.56
Then write it to a file:
with open('some_file.txt', 'w') as fw:
fw.write(s)
You can now further simplify this to:
with open('some_file.txt', 'w') as fw:
fw.write('\n'.join(d.keys()))

json_data["result"]["terms"].keys() should give you all the keys.
You should read the documentation for how to open and write to file. It is very straight forward in python. Assuming you are using python 2.7, here is the link: https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files
It would be something like this:
f = open(filename, 'w')
f.write(json_data["result"]["terms"].keys())
f.close()

A dictionary has, by definition, an arbitrary number of keys. There is no "the key". You have the keys() method, which gives you a python list of all the keys, and you have theiteritems() method, which returns key-value pairs, so
for key, value in mydic.iteritems() :
thefile.write("%s\n" % key)
Or simply you can do this
for key in mydic.keys():
thefile.write("%s\n" % key)

Related

Python - get a LIST value from field in JSON

I have a JSON file containing three fields: 2 are strings and third one is field containing a list of values.
{ "STREAM": "stream",
"BASIS_STREAM": "basis",
"PATHS": "[/opt/path1,/opt/path2]"
}
Now I load that JSON
with open('/pathToJsonFile.json', 'r') as f:
data = json.load(f)
Now I want to get those values.
stream=str(data["STREAM"])
basis=str(data["BASIS_STREAM"])
paths=data["BASE_PATHS"]
The issue is that paths is also threated as String, although I have to use it as a list. I am converting with str function other fields because of the Unicode. Code must be in python2.
Thanks a lot!
Say you have a file called data.json with the following contents:
{
"STREAM": "stream",
"BASIS_STREAM": "basis",
"PATHS": "[/opt/path1,/opt/path2]"
}
Maybe you could use str.split after calling json.load:
with open('data.json', 'r') as f:
data = json.load(f)
print 'data = %s' % data
stream = str(data['STREAM'])
basis = str(data['BASIS_STREAM'])
paths = [str(u_s) for u_s in data['PATHS'][1:-1].split(',')]
print 'stream = %s' % stream
print 'basis = %s' % basis
print 'paths = %s' % paths
Output:
data = {u'PATHS': u'[/opt/path1,/opt/path2]', u'BASIS_STREAM': u'basis', u'STREAM': u'stream'}
stream = stream
basis = basis
paths = ['/opt/path1', '/opt/path2']
Your /opt/path1 and /opt/path2 should be in a quotation marks to be converted in a list. If your PATHS always have a similar template such as "[/XXX,/YYY,/ZZZ,/TTT,/KKK]" the following code should also help. I have converted your data as "['/XXX','/YYY','/ZZZ','/TTT','/KKK']" so that it can be easily converted to a list using ast library. Please see the code as following:
import json
import ast
with open("text_text.json") as f:
data = json.load(f)
print(data["PATHS"]) # Your data
for i in data["PATHS"]:
if i == "[":
data["PATHS"] = data["PATHS"].replace("[", "['")
elif i == ",":
data["PATHS"] = data["PATHS"].replace(",/", "','/")
elif i == "]":
data["PATHS"] = data["PATHS"].replace("]", "']")
#print(data["PATHS"])
print(type(data["PATHS"]))
print(data["PATHS"]) #converted to a data which can be converted to a list.
data_paths = ast.literal_eval(data["PATHS"]) # ast is used to convert str to list.
print(data_paths) # 'list' data
print(type(data_paths))
See the output of the code:
It should also work if your PATH has more data as following:

How to Change dictionary values in python file from another file

I would like to change values in a Dict in another file. File1.py contains the code to edit the Dict, File2.py contains the Dict itself.
File1.py is generating a code to replace BTOK values only.
File1.py:
with open('file2.py', 'r') as file :
filedata = file.read()
print (filedata.str(BTK['btk1']))
for line in filedata:
line['btk1'] = BTok
with open('file2.py', 'w') as file:
file.write(line)
File2.py:
c = {
'id' : 'C80e3ce43c3ea3e8d1511ec',
'secret' : 'c10c371b4641010a750073925b0857'
}
rk = {
't1' : 'ZTkwMGE1MGEt',
}
BTK = {
'BTok' : '11eyJhbGc'
}
If you want to do this reliably, that is, so it works whether your strings are quoted with ', " or """, for whatever values they have and whatever newlines you want to put around values, then you may want to use ast to parse the source code and modify it. The only inconvenient with this is that module cannot, by itself, generate code, so you would need to install some additional dependency such as astor, for what is essentially a rather menial task. In any case, here is how you could do it that way:
import ast
import astor
# To read from file:
# with open('file2.py', 'r') as f: code = f.read()
code = """
c = {
'id' : 'C80e3ce43c3ea3e8d1511ec',
'secret' : 'c10c371b4641010a750073925b0857'
}
rk = {
't1' : 'ZTkwMGE1MGEt',
}
BTK = {
'BTok' : '11eyJhbGc'
}
"""
# Value to replace
KEY = 'BTok'
NEW_VALUE = 'new_btok'
# Parse code
m = ast.parse(code)
# Go through module statements
for stmt in m.body:
# Only look at assignments
if not isinstance(stmt, ast.Assign): continue
# Take right-hand side of the assignment
value = stmt.value
# Only look at dict values
if not isinstance(value, ast.Dict): continue
# Look for keys that match what we are looking for
replace_idx = [i for i, k in enumerate(value.keys)
if isinstance(k, ast.Str) and k.s == KEY]
# Replace corresponding values
for i in replace_idx:
value.values[i] = ast.Str(NEW_VALUE)
new_code = astor.to_source(m)
# To write to file:
# with open(`file2.py', 'w') as f: f.write(new_code)
print(new_code)
# c = {'id': 'C80e3ce43c3ea3e8d1511ec', 'secret':
# 'c10c371b4641010a750073925b0857'}
# rk = {'t1': 'ZTkwMGE1MGEt'}
# BTK = {'BTok': 'new_btok'}

how do I add a string to a json value in python 3

So I'm trying to setup json so i can store data in-between user sessions I like a name but i don't know how to add or change a specific value in an external json file like for example {"name": ""} how do i fill that "" for the json file using python?
I have already tried to use dumps and all the tutorials use dumps
the json in another file
{
"human_name": "",
"oracle_name": "",
"human_age": "",
"human_gender": "",
"oracle_gender": ""
}
the python
import json
with open('data.json', '+') as filedata:
data = filedata.read()
used_data = json.loads(data)
if str(used_data(['human_name'])) == "":
print("what is your name")
name = input()
json.dumps(name)
if str(used_data(['oracle_name'])) == "":
print("what is my name")
oracle_name = input()
json.dumps(oracle_name)
print(str(['human_name']))
The expected result is when I print the data it displays input, but when i run it it goes
File "rember.py", line 3, in
with open('data.json', '+') as filedata: ValueError: Must have exactly one of create/read/write/append mode and at most one plus
Try this code.
json.loads loads the entire json string as a python dict object. The values in a dict are changed/added using dict[key] = value. You can't call a dict object to change its value.
The json.dumps method serializes an object to a JSON formatted str. Which you can then write into the same file or a different file based on your requirement.
import json
with open('data.json', 'r') as filedata:
data = filedata.read()
used_data = json.loads(data)
if used_data['human_name'] == "":
print("what is your name")
name = input()
used_data['human_name'] = name
if used_data['oracle_name'] == "":
print("what is my name")
oracle_name = input()
used_data['oracle_name'] = oracle_name
print(used_data)
with open('data.json', 'w') as filewrite:
filewrite.write(json.dumps(used_data, indent=4))
Basically what you need to do is load json file as dictionary, add value, and save it.
import json
with open('./data.json', 'r') as f:
d = json.load(f)
d['human_name'] = 'steve'
d['oracle_name'] = 'maria'
with open('./data.json', 'w') as f:
json.dump(d, f, indent=4)

Dump content of JSON in specific format

I am need to actually create a Windows INI using Python scipt file of below format:
AGENTIP = 1.2.3.4,
VARFILE = C:\Users\output\temp.out
INFOFILE= C:\Users\output\info.out
SYNTEST = Run:Level1/Get
CMDMODE = RUNTESTSUITE
And Below is my Python code where I have the data in JSON string and then dump the content in a file:
def change_test_details(self, ver, level, grp):
data = {"AGENTIP" : "1.2.3.4", "VARFILE" : "C:\\Users\\output\\temp.out", "INFOFILE" : "C:\\Users\\output\\info.out", "SYNTEST" :"Run:Level1/Get", "CMDMODE" :"RUNTESTSUITE"}
data["SYNTEST"] = ver + ":" + level + "/" + grp
with open("a.txt", 'w') as outfile:
json.dump(data, outfile,indent=2)
When the method is called with below param:
"BETA" "Level5" "Set"
The final output if the file is
{
"AGENTIP": "1.2.3.4",
"VARFILE": "C:\\Users\\output\\temp.out",
"INFOFILE": "C:\\Users\\output\\info.out",
"SYNTEST": "\"BETA\":\"Level5\"/\"Set\"",
"CMDMODE": "RUNTESTSUITE"
}
There is '{ .. }' braces and extra double quotes and '\' and expected value of SYNTEST should be BETA:Level5/Set ?
How can change the JSON string to required format?
json.dump() prints an object as a json formatted string. So when you dump that into outfile you get a json string in the file, which is how it's supposed to behave.
What you want is to iterate over the items and print them according to the format you want.
def change_test_details(ver, level, grp):
data = {"AGENTIP" : "1.2.3.4", "VARFILE" : "C:\\Users\\output\\temp.out", "INFOFILE" : "C:\\Users\\output\\info.out", "SYNTEST" :"Run:Level1/Get", "CMDMODE" :"RUNTESTSUITE"}
data["SYNTEST"] = ver + ":" + level + "/" + grp
with open("a.txt", 'w') as outfile:
for k, v in data.items():
outfile.write(f'{k} = {v}\n')
change_test_details("BETA", "Level5","Set")
When you run this, a.txt looks something like:
AGENTIP = 1.2.3.4
VARFILE = C:\Users\output\temp.out
INFOFILE = C:\Users\output\info.out
SYNTEST = BETA:Level5/Set
CMDMODE = RUNTESTSUITE

KeyError: u'somestring' Json

I am trying to make a point system for my Twitch bot and I am encountering KeyErrors when trying to make a new entry for some odd reason. Here is my code:
import urllib2, json
def updateUsers(chan):
j = urllib2.urlopen('http://tmi.twitch.tv/group/user/' + chan + '/chatters')
j_obj = json.load(j)
with open('dat.dat', 'r') as data_file:
data = json.load(data_file)
for usr in j_obj['chatters']['viewers']:
data[usr]['Points'] = "0" # Were the KeyError: u'someguysusername' occurs
with open('dat.dat', 'w') as out_file:
json.dump(data, out_file)
updateUsers('tryhard_clan')
If you want to see the Json itself go to http://tmi.twitch.tv/group/user/tryhard_clan/chatters
I'm storing user data in a file in this format:
{"users": {"cupcake": {"Points": "0"}}}
a slightly more concise form than #Raunak suggested:
data.setdefault (usr, {}) ['Points'] = "0"
that will set data[usr] to an empty dict if it's not already there, and set the 'Points' element in any case.
It happens variable usr doesn't resolve to an existing key in data. Do this instead:
if usr not in data:
data[usr] = {}
data[usr]['Points'] = "0"

Categories

Resources