Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading json file
#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, then open this script and do run module. But no output. Any help would be greatly appreciated.

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)
Larz60+ write Nov-25-2020, 08:59 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Added for you this time. Please use bbcode tags on future posts.
Reply
#2
Please share sample JSON file which contains examples of each data structure used.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with reading json file hhchenfx 5 316 Jul-07-2021, 01:58 PM
Last Post: hhchenfx
  [Solved] Reading every nth line into a column from txt file Laplace12 7 506 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  help with project of reading and searching big log file korenron 6 275 Jun-24-2021, 01:57 PM
Last Post: korenron
  Helps with reading csv file - 3 methods hhchenfx 4 496 May-13-2021, 04:15 AM
Last Post: buran
  Subprocess.Popen() not working when reading file path from csv file herwin 13 1,172 May-07-2021, 03:26 PM
Last Post: herwin
  Request for Python code - Features extraction from JSON file (cuckoo) thinker 1 441 Apr-07-2021, 04:40 PM
Last Post: Larz60+
  find the header location in a .bin file without reading the whole file at a time SANJIB 0 482 Mar-05-2021, 04:08 PM
Last Post: SANJIB
  Reading a csv file Led_Zeppelin 2 658 Feb-26-2021, 05:48 AM
Last Post: buran
  reading a csv file Led_Zeppelin 3 853 Feb-19-2021, 02:16 PM
Last Post: Led_Zeppelin
  Sort data from JSON file Dummy_in_programming 2 547 Jan-04-2021, 06:17 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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