I was working on a piece of code that reads a json file ever second or so in accordance with a pyqt5 graph. This was my code:
import csv
from getmac import get_mac_address as gma
import time
macaddr = gma()
mac_name = macaddr.replace(":", "")
weathersensor_id = "BME20"
rainsensor_id = "GCCG41"
def readJSONLatestAllMQTT():
with open(f"{mac_name}_{weathersensor_id}.json", "r") as myfile:
dataRead = json.load(myfile)
time.sleep(0.01)
return dataRead, True
When I went to access an element of the json file by doing something like print(dataRead["Temperature"]), I got an error message that said that dataRead was being output as a string, not a dict/json file like I intented. How would I go about turning dataRead into a dict instead of a string?
Here is the json file for some background:
"{\"dateTime\": \"2021-06-18 07:47:33.710631\", \"Temperature\": 69.26586754195563, \"Pressure\": 346.63102628054014, \"Humidity\": 80.54066707990641, \"Altitude\": 125.3640651860123}"
I believe you are having this problem because your json file is a string instead of a dictionary, I think it should be:
{"dateTime": "2021-06-18 07:47:33.710631", "Temperature": 69.26586754195563, "Pressure": 346.63102628054014, "Humidity": 80.54066707990641, "Altitude": 125.3640651860123}
instead of:
"{\"dateTime\": \"2021-06-18 07:47:33.710631\", \"Temperature\": 69.26586754195563, \"Pressure\": 346.63102628054014, \"Humidity\": 80.54066707990641, \"Altitude\": 125.3640651860123}"
Related
I have a function:
def save(path,data):
file = open(path,'w',encoding='utf-8')
file.write(json.dumps(data))
file.close()
It saves the file, but without tabbing. Everything goes in one line.
save('1.json',{"1":"222"})
How do I make it save in tab format?
{
"1":"222"
}
Use indent argument with json.dumps as below:
import json
def save(path, data):
file = open(path,'w',encoding='utf-8')
file.write(json.dumps(data, indent=4))
file.close()
save('1.json',{"1":"222"})
json.dumps have an indent keyword argument to specify the indentation of the output.
def save(path, data, indent=2):
file = open(path,'w',encoding='utf-8')
file.write(json.dumps(data, indent=indent))
file.close()
save('1.json',{"1":"222"})
1.json
{
"1": "222"
}
use params indent
def save(path,data):
with open(path,'w',encoding='utf-8') as f:
f.write(json.dumps(data,indent=4))
I have a bunch of json files with multiple lines who look like this:
file1
{"id":1,"name":"Eric","height":1.80, ...},
{"id":2,"name":"Bob","height":1.90, ...}
...
file2
{"id":3,"name":"Jenny","height":1.50, ...},
{"id":4,"name":"Marlene","height":1.60, ...}
...
I want to build a generator to yield each line as a dictionary. My current code:
from typing import Iterator, Dict, Any, Optional
import io
import os
def json_gen2(file_list: list) -> Iterator[Dict[str, Any]]:
import json
for file in file_list:
with open(file) as json_file:
data = []
for line in json_file:
data = json.load(line)
if not data:
break
yield data
datapath = os.path.normcase(os.getcwd()) + '/data/log_data'
file_list = get_files(datapath) # create path list of json files
jsonfile = json_gen2(file_list)
next(jsonfile)
i get the following
Error Message
pls help :)
Oops, I misread. You are doing the same thing I was saying. Your error is due to using 'load' instead of 'loads'. Each line returned by
for line in json_file:
data = json.load(line)
is a string, and you're attempting to read it as a file pointer.
I'm trying to remove the very first character (") from a file which contains a JSON String. I'm using Python for this. Below is my code:
jsonOutput = 'JsonString_{}.{}'.format(str(uuid.uuid1()), "json")
jsonOutput_File = os.path.join(arcpy.env.scratchFolder, jsonOutput)
with open(jsonOutput_File, 'w') as json_file:
json.dump(jsonString, json_file)
// I was able to remove the very last character using the code below
with open(jsonOutput_File, 'r+') as read_json_file:
read_json_file.seek(-1, os.SEEK_END)
read_json_file.truncate()
Basically when I dump the JSON String to a file, the String is getting surrounded by double quotes. I'm trying to remove these double quotes from the first & last position of the file.
If you already have a JSON string, simply write it to the file.
Encoding the JSON string to JSON again using json.dump() is a bad idea and will not be fixed as simple as removing a leading and a trailing quote.
Consider the following minimal and complete example:
import json
import os
import uuid
myobject = {"hello": "world"}
jsonString = json.dumps(myobject)
jsonOutput = 'JsonString_{}.{}'.format(str(uuid.uuid1()), "json")
jsonOutput_File = os.path.join("d:\\", jsonOutput)
with open(jsonOutput_File, 'w') as json_file:
json.dump(jsonString, json_file)
The output is a file with the content:
"{\"hello\": \"world\"}"
Removing the quotes will not make it valid JSON.
Instead, avoid the duplicate JSON creation, either by removing json.dumps() which converts the object to JSON one time, or by removing json.dump(), which does it a second time.
Solution 1:
import json
import os
import uuid
myobject = {"hello": "world"}
# <-- deleted line here
jsonOutput = 'JsonString_{}.{}'.format(str(uuid.uuid1()), "json")
jsonOutput_File = os.path.join("d:\\", jsonOutput)
with open(jsonOutput_File, 'w') as json_file:
json.dump(myobject, json_file) # <-- changed to object here
Solution 2:
import json
import os
import uuid
myobject = {"hello": "world"}
jsonString = json.dumps(myobject)
jsonOutput = 'JsonString_{}.{}'.format(str(uuid.uuid1()), "json")
jsonOutput_File = os.path.join("d:\\", jsonOutput)
with open(jsonOutput_File, 'w') as json_file:
json_file.write(jsonString) # <-- Note this line
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)
I have a python file with a lot of code and lists. I need to add column in particular list.
My questions are - how can I load particular list from .py file? And, how can I add element into particular list?
Here in my code:
import os, datetime, json
login1 = os.environ["login"].split('\')[1].strip()
login = login1.split('.')[0].strip()
USERID = str(os.environ['USERID'])
header = login + USERID + '.json'
with open("d:\\python\\monitor.py", "r") as infile:
data = infile.readlines()
#here I need to load a "dev_personal_files" list and append additional element
Content of monitor.py file
#some python code
dev_personal_files = [
'Yura.json',
'Sasha.json'
]
staging_files = [
#'ple.json',
'retailReleaseServer.json',
'topaz.json',
'ple2.json',
#'klub.json',
'gaabtMX.json'
]
staging_files2 = [
'retailDemo.json',
'resort.json',
'jhnkljkl.json',
'hbjk,nm,.json',
'bnbnj,jnk,.json'
]
#some python code
What I want to add into "dev_personal_files" list (list within monitor.py file):
dev_personal_files = [
'NEWRECORD.json',
'Yura.json',
'Sasha.json'
]
Getting a list (or any object) from a .py file is done by importing the file:
import mypyfile # <- leave off the .py
importedlist = mypyfile.dev_personal_files
Or you can just import the object itself:
from mypyfile import dev_personal_files
dev_personal_files.extend(my_list_of_extra_items)
However the changes to the list will be lost after you end your python session.
If you want to permanently store changes to the list, save it in a format like json, not in a py file.
import json
# Read data from the file
with open('myfile.json') as f:
my_list = json.load(f)
# Add an item to your list
my_list.append('foo')
# Save data to the file
with open('myfile.json', 'w') as f:
json.dump(my_list)