Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
json function use
#1
I'm new to python so I need a bit of direction. I've got some python code given to me to run against some quite complicated json files. I followed directions but cannot get the code to work correctly so I'm obviously missing something. I have decades of programming experience but not in this new environment. I used fortran, cobol, pascal etc. Things a bit different now. SO anyway I downloaded python 3.8.2 onto my pc. Loaded the python script and json file into the same path. Normally when runnign code I'd like to see a debug of what the code is doing. How do I turn that on? I cannot tell where python is trying to read from. Here's the script. It seems you need a wildcard to input NAME. Not sure where to do this. I open idle, do file new, paste in the script and save then do run module. But no output. Any help would be greatly appreciated. Listing from debug at end. As I said I'm new to python so the errors have little meaning to me. Thanks.
import json



def findfraud(NAME):
    with open(NAME + '.json', encoding="utf8") as f:
        x = json.load(f)
    TotalVotesLost = 0
    for i in range(len(x["data"]["races"][0]["timeseries"])):
        if i != 0 and x["data"]["races"][0]["timeseries"][i]["votes"] * x["data"]["races"][0]["timeseries"][i]["vote_shares"]["trumpd"] < x["data"]["races"][0]["timeseries"][i-1]["votes"] * x["data"]["races"][0]["timeseries"][i-1]["vote_shares"]["trumpd"]:
            if x["data"]["races"][0]["timeseries"][i]["votes"] * x["data"]["races"][0]["timeseries"][i]["vote_shares"]["bidenj"] > x["data"]["races"][0]["timeseries"][i-1]["votes"] * x["data"]["races"][0]["timeseries"][i-1]["vote_shares"]["bidenj"]:
                print ("Index : " + str(i) + " Past Index : " + str(i-1))
                print (x["data"]["races"][0]["timeseries"][i]["votes"] * x["data"]["races"][0]["timeseries"][i]["vote_shares"]["trumpd"] - x["data"]["races"][0]["timeseries"][i-1]["votes"] * x["data"]["races"][0]["timeseries"][i-1]["vote_shares"]["trumpd"])
                TotalVotesLost += x["data"]["races"][0]["timeseries"][i]["votes"] * x["data"]["races"][0]["timeseries"][i]["vote_shares"]["trumpd"] - x["data"]["races"][0]["timeseries"][i-1]["votes"] * x["data"]["races"][0]["timeseries"][i-1]["vote_shares"]["trumpd"]
    print (str(str(TotalVotesLost)  + " Flo"))

def findfraud2(NAME):
    with open(NAME + '.json', encoding="utf8") as f:
        x = json.load(f)
    TotalVotesLost = 0
    for i in range(len(x["data"]["races"][0]["timeseries"])):
        if i != 0 and x["data"]["races"][0]["timeseries"][i]["votes"] < x["data"]["races"][0]["timeseries"][i-1]["votes"]:
            TotalVotesLost += x["data"]["races"][0]["timeseries"][i]["votes"] - x["data"]["races"][0]["timeseries"][i-1]["votes"]
    print (TotalVotesLost)
[DEBUG ON]
>>>
= RESTART: C:/Users/Paul/AppData/Local/Programs/Python/Python38-32/findfraud.py
Traceback (most recent call last):
File "C:/Users/Paul/AppData/Local/Programs/Python/Python38-32/findfraud.py", line 1, in <module>
import json
File "<frozen importlib._bootstrap>", line 988, in _find_and_load
File "<frozen importlib._bootstrap>", line 148, in __enter__
File "<frozen importlib._bootstrap>", line 174, in _get_module_lock
File "<frozen importlib._bootstrap>", line 59, in __init__
File "<frozen importlib._bootstrap>", line 59, in __init__
File "C:\Users\Paul\AppData\Local\Programs\Python\Python38-32\lib\bdb.py", line 88, in trace_dispatch
return self.dispatch_line(frame)
File "C:\Users\Paul\AppData\Local\Programs\Python\Python38-32\lib\bdb.py", line 112, in dispatch_line
self.user_line(frame)
File "C:\Users\Paul\AppData\Local\Programs\Python\Python38-32\lib\idlelib\debugger.py", line 24, in user_line
self.gui.interaction(message, frame)
AttributeError: '_ModuleLock' object has no attribute 'name'
[DEBUG ON]
>>>
Reply
#2
Not sure what the issue is, but I would like to offer some design advice. If you move the file opening out of the functions and use x["data"]["races"][0]["timeseries"] as the argument, you can pare down all the data retrieval and make it easier to read.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Write lambda function in pyhhon to coy data from multiple JSON into a single JSON fil anandmn85 2 4,186 Apr-19-2018, 05:56 AM
Last Post: anandmn85

Forum Jump:

User Panel Messages

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