Can't use my local json file text as intiger - python

import multiprocessing
import urllib.request
import json
with open("crypto.json") as f:
data = json.loads(f)
result = data
print(type(result))
resultbtc = int(result['User']['BTC'])
resultdash = int(result['User']['DASH'])
resulteth = int(result['User']['ETH'])
url = "https://min-api.cryptocompare.com/data/pricemulti?fsyms=ETH,DASH,BTC&tsyms=BTC,EUR& api_key=9a96785fb79da776270b5ffc9e989d9092bbe24d23472e107301cec5ff8a82f3"
data = urllib.request.urlopen(url)
html = data.read()
html = html.decode()
o = json.loads(html)
btcv = o['BTC']['EUR']
dashv = o['DASH']['EUR']
ethv = o['ETH']['EUR']
fresbtc = btcv * resultbtc['BTC']
fresdash = dashv * resultdash['DASH']
freseth = ethv * resulteth['ETH']
print ("Ο χρήστης",result['Name'],"εχει",fresbtc,"€ σε BITCOIN",freseth,"€ σε ETHEREUM",fresdash,"€ σε DASH")
JSON file:
[
{
"Name" : "Jonh Smith",
"BTC" : "23",
"ETH" : "345",
"DASH" : "1045"
}
]
I want to extract the values of BTC, ETH and DASH and use them as integers to be able to print their values with real time data but I get this error
Traceback (most recent call last):
File "C:\UniPapei\Εισαγωγη στην επιστημη των υπολογιστων\New folder\Εργ 4\bitcoinerg.py", line 20, in
data = json.loads(f)
File "C:\Users\Argyris\AppData\Local\Programs\Python\Python39\lib\json_init_.py", line 339, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper

The problem is you're trying to execute loads on a file handle, not the contents of the file. Read the file's contents and pass that to json.loads()
with open("crypto.json") as f:
data = json.loads(f.read())
result = data
print(type(result))

json.loads() does not accept the file object. Instead use json.load()
with open("crypto.json") as f:
data = json.load(f)
result = data

Related

Error while writing API result to another JSON file

I am working on AZURE Cognitive API Search. While getting the result from API, I want to write it into a new JSON File. I tried to access the analyse_result variable with the line but it does not work. It shows that the object is not JSON Serializable. My code is-
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials
from array import array
import os
from PIL import Image
import sys
import time
import json
import csv
subscription_key = ""
endpoint = ""
computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))
def azure_ocr_api(): #image_url
local_image_url = r"E:\Bank of Baroda\BOB IMAGE\Cheque309086.jpeg"
# read_response = computervision_client.read_in_stream(open("./Images/" + image_url,'rb'), raw=True)
read_response = computervision_client.read_in_stream(open(local_image_url,'rb'), raw=True)
# Get the operation location (URL with an ID at the end) from the response
read_operation_location = read_response.headers["Operation-Location"]
# Grab the ID from the URL
operation_id = read_operation_location.split("/")[-1]
# Call the "GET" API and wait for it to retrieve the results
while True:
read_result = computervision_client.get_read_result(operation_id)
if read_result.status not in ['notStarted', 'running']:
break
time.sleep(1)
list = []
if read_result.status == OperationStatusCodes.succeeded:
for text_result in read_result.analyze_result.read_results:
for line in text_result.lines:
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(line, f, ensure_ascii=False, indent=4)
# print(list)
# pass
# return list
azure_ocr_api()
print("End of Computer Vision quickstart.")
The code shows a error like this -
Traceback (most recent call last):
File "e:\Bank of Baroda\m.py", line 44, in <module>
azure_ocr_api()
File "e:\Bank of Baroda\m.py", line 40, in azure_ocr_api
json.dump(line, f, ensure_ascii=False, indent=4)
File "C:\Users\Clasher\anaconda3\lib\json\__init__.py", line 179, in dump
for chunk in iterable:
File "C:\Users\Clasher\anaconda3\lib\json\encoder.py", line 438, in _iterencode
o = _default(o)
File "C:\Users\Clasher\anaconda3\lib\json\encoder.py", line 179, in default
TypeError: Object of type Line is not JSON serializable
Please help.

TypeError: list indices must be integers, not str by json data

The whole code means scan all json files( 300+) and converter json to html.
But some titles have some special characters, I have replace those first.
But
Traceback (most recent call last):
File ".\json2html.py", line 30, in <module>
d["title"] = re.sub("[|\^&+\-%*?/=!>]", "-", d["title"])
TypeError: string indices must be integers
I've already googled 3 hours, but no solution for my case ,
such as this one:
Parsing JSON with Python: TypeError: list indices must be integers, not str
Python and JSON - TypeError list indices must be integers not str
my Code :
import json
import re
from glob import glob
for file_name in glob("*.json"):
fi = open(file_name, 'r')
data = json.load(fi)
for a in data.values():
for d in a:
d["title"] = re.sub("[|\^&+\-%*?/=!>]", "-", d["title"])
fo = open(data["title"] + ".html", 'w')
fo.write(str(data["body_html"]))
fi.close()
fo.close()
I tried :
for a in data:
a["title"] = re.sub("[|\^&+\-%*?/=!>]", "-", a["title"])
But still got error.
Tried :
dataTitle = data[0]["title"]
dataTitle = re.sub("[|\^&+\-%*?/=!>]", "-", dataTitle )
fo = open(dataTitle + ".html", 'w')
still got error:
Traceback (most recent call last):
File ".\json2html.py", line 28, in <module>
dataTitle = data[0]["title"]
KeyError: 0
Json :
{
"body_html": "<div><head></head><body><div class=\"lake-content-editor-core lake-engine lake-typography-traditional\" data-lake-element=....</a></div></div></body></div>",
"slug": 6643849,
"title": "idea/project?task->things<-tools%"
}
Sample json file:
https://www.dropbox.com/s/ks9cc1qzegc7t5j/cq4coz.json?dl=0
You don't need to iterate inside your json since it's a single object:
data = json.load(fi)
data["title"] = re.sub("[|\^&+\-%*?/=!>]", "-", data["title"])

Replace values from YAML in JSON file - Python 3

I want to replace the values from YAML file into a JSON file using Python 3.
I have many JSON files where I want to get the values from a master YAML file and replace only certain values like source server ip, email, hostname.
E.g. I have this YAML file (mast_conf.yaml):
- sourcesystem1:
sourceServer: 1.2.3.500
MailTo: gokul#gmail.com
- sourcesystem2:
sourceServer1: 2.2.3.500
sourceServer2: 3.2.3.500
MailTo: gokul#gmail.com
A JSON file (sourcesystem1.json):
{
"source":"sourcesystem1",
"frequency":"daily",
"sourceServer":"1.2.1.2",
"hostName":"1.2.1.3",
"fileFormat":"csv",
"delimiterType":"semicolon"
}
Another JSON file (sourcesystem2.json):
{
"source":"sourcesystem2",
"frequency":"daily",
"sourceServer":"1.2.3.2",
"hostName":"1.2.1.7",
"fileFormat":"csv",
"delimiterType":"commaseperated"
}
Below is my code I am trying out to parse the value from the json file
import json
import yaml
with open("master_conf.yaml", 'r') as f:
yaml_config = yaml.safe_load(f)
yaml_config = {
list(config.keys()[0]): list(config[config.keys()[0]])
for config in yaml_config
}
json_files = ( "sourcesystem1.json",
"sourcesystem2.json",
)
for json_file in json_files:
with open(json_file, "r") as f:
sourcesystem_conf = json.load(f)
sourcesystem = sourcesystem_conf["source"]
if sourcesystem in yaml_config:
for key, value in yaml_config[sourcesystem].items():
sourcesystem_conf[key] = value
with open(json_file, "w") as f:
json.dump(sourcesystem_conf, f, indent=2)
I am getting the below error by program
TypeError: 'dict_keys' object does not support indexing
When I run indivudually I get this issue for yaml
>>> yaml_config = { ... config.keys()[0]: config[config.keys()[0]] ... for config in yaml_config ... } Traceback (most recent call last): File "<stdin>", line 3, in <module> File "<stdin>", line 3, in <dictcomp> TypeError: 'dict_keys' object is not subscriptable >>>
Is there easier method to achieve my end goal where I want to replace the values in the JSON file from the Yaml configuration file
This is needed to update 1000s of Json file in a automated way for updating it from a master Yaml file
The easiest way is to use pyyaml, see Jon's answer.
Then you can load you yaml file using it :
>>> import yaml
>>> yaml_config = yaml.safe_load(yaml_file)
>>> yaml_config
[{'sourcesystem1': {'MailTo': 'gokul#gmail.com', 'sourceServer': '1.2.3.500'}},
{'sourcesystem2': {'MailTo': 'gokul#gmail.com',
'sourceServer1': '2.2.3.500',
'sourceServer2': '3.2.3.500'}}]
It will be easier to manipulate a dict with source systems as keys.
In python 2 aDict.keys() returns a list so the following will work :
>>> yaml_config = {
config.keys()[0]: config[config.keys()[0]]
for config in yaml_config
}
>>> yaml_config
{'sourcesystem1': {'MailTo': 'gokul#gmail.com', 'sourceServer': '1.2.3.500'},
'sourcesystem2': {'MailTo': 'gokul#gmail.com',
'sourceServer1': '2.2.3.500',
'sourceServer2': '3.2.3.500'}}
In python 3 aDict.keys() no longer returns a list so you can simply use a for loop :
yaml_config = {}
for config in yaml_config_raw:
source = [key for key in config][0]
yaml_config[source] = config[source]
Then you can just iterate over your json files to update them :
import json
import yaml
with open("mast_conf.yaml", 'r') as f:
yaml_config_raw = yaml.safe_load(f)
yaml_config = {}
for config in yaml_config_raw:
source = [key for key in config][0]
yaml_config[source] = config[source]
json_files = (
"sourcesystem1.json",
"sourcesystem2.json",
)
for json_file in json_files:
with open(json_file, "r") as f:
sourcesystem_conf = json.load(f)
sourcesystem = sourcesystem_conf["source"]
if sourcesystem in yaml_config:
for key, value in yaml_config[sourcesystem].items():
sourcesystem_conf[key] = value
with open(json_file, "w") as f:
json.dump(sourcesystem_conf, f, indent=2)

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)

How do I convert Python output results to JSON string using Python

This is my function call
if __name__ == '__main__':
a = head_tail()
b = data_info_for_analysis()
c = data_visualization_chart()
d = missing_values_duplicates()
e = mapping_yes_no()
f = one_hot_encoding()
g = outlier_identification()
out2 = removing_outliers()
h = droping, features = removing_unwanted_columns(out2)
df_telecom_test, df_telecom_train, probs, clf = random_model_predictions(droping, features)
i = logistic_model_prediction(df_telecom_train, df_telecom_test, features)
j = decision_model_prediction(df_telecom_train, df_telecom_test, features)
k = fpr_tpr_thresholds(df_telecom_test, probs, clf, features)
I am trying to save that object as a json file
filter = "JSON File (*.json)|*.json|All Files (*.*)|*.*||"
filename = a.SaveFileName("Save JSON file as", filter)
if filename:
with open(filename, 'w') as f:
json.dump(a, f)
I am getting this below error
Traceback (most recent call last):
File "/home/volumata/PycharmProjects/Churn-Analysis/sample-object-json.py", line 429, in <module>
filename = a.SaveFileName("Save JSON file as", filter)
AttributeError: 'NoneType' object has no attribute 'SaveFileName'
I have tried another method also
def head_tail():
### Head of the data
print(df_telecom.head(5))
### Tail of the data
print(df_telecom.tail(5))
code_obj = head_tail()
dis.disassemble(code_obj)
After trying this above method, getting this error
cell_names = co.co_cellvars + co.co_freevars
AttributeError: 'NoneType' object has no attribute 'co_cellvars'
For serialising a pandas.DataFrame into JSON you can use its to_json() method. There are different formatting options:
>>> df
0 1
0 a b
1 c d
>>> df.to_json()
'{"0":{"0":"a","1":"c"},"1":{"0":"b","1":"d"}}'
>>> df.to_json(orient='values')
'[["a","b"],["c","d"]]'
You question is very unclear. If you just want to convert some data from python-standard-types you can simply use json.dump:
someResults = { ... }
import json
with open("file.txt", "w") as f:
json.dump(someResults, f, indent=4)

Categories

Resources