Python Forum
can you please help me with this python code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can you please help me with this python code
#1
import json

try:
    with (open("log.txt")) as file:
        data = json.load(file)
    
    print("data has been successfully retrieved")
except json.decoder.JSONDecodeError:
    print("There was a problem accessing the equipment data")
    
#import json

#with open("log.txt") as file:
	#data = json.load(file)

#print("Equipment data has been successfully retrieved.") #comment the code above and than uncomment it to have the code above work
#this code below doesn't work 
i'm not sure if this code uses the file log.txt
Reply
#2
If no path is given to log.txt then most be in same as you run .py from.
With path eg will look like this if use Windows r'C:\your_folder\log.txt
Also when use json module it most be validate json,therefore should use .json files.
It will work if .txt has validate json content,but to avoid confusion it should be saved as .json
Then it look like this if make a working test.
# json_test.py in same folder as data.json
import json

try:
    with open("data.json") as file:
        data = json.load(file)
        print(data) # If work will return a Python dictionary
        print("data has been successfully retrieved")
except json.decoder.JSONDecodeError:
    print("There was a problem accessing the equipment data")
Reply
#3
import json

try:
    with (open("log.txt", "r")) as f:
        pass
        data = json.load(f)
except json.decoder.JSONDecodeError:
    f = open("log.txt", 'r')
    print(f.read())
    f.close()
    
#import json

#with open("log.txt") as file:
	#data = json.load(file)

#print("Equipment data has been successfully retrieved.") #comment the code above and than uncomment it to have the code above work
#this code below doesn't work 
i think i fixed the code it opens the txt file but it needs to change the text of the file to something different
Reply
#4
(May-02-2022, 08:56 PM)MetsxxFan01 Wrote: i think i fixed the code it opens the txt file but it needs to change the text of the file to something different
It's a kind of a fix,if it's just a ordinary log file with no json content then only need line 8 to 10.
Also it good practice to use with open,then don't need to close file.
with open('log.txt') as f:
    print(f.read)
Reply
#5
import json

def write_json(data, filename="log.json"):
    with open(filename,'w') as file:
        json.dump(data, file, indent=4)

with open("log.json",'w') as json_file:
    data = json.load(json_file)
    temp = data["names"]
    add_letters = {"A", "M", "A", "I", "C", "O", "N", "Z", "E"}
    temp.append(add_letters)

write_json(data)
how do i add append to the code?
Reply
#6
(May-03-2022, 07:45 AM)MetsxxFan01 Wrote: how do i add append to the code?
When read file don't use 'w' in line 8.
data is a dictionary and you access key names,so it depend on what it return.
Look at tutorial for dictionary how to append/insert/update eg something like this.
When dictionary is finish you Serializing(dump) to json.
Example:
import json

dictionary ={
    "name": "sathiyajith",
    "rollno": 56,
    "cgpa": 8.6,
    "phonenumber": "9976770500"
}

with open("sample.json", "w") as outfile:
    json.dump(dictionary, outfile)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020