1

I've been stuck on this for like an hour and I just can't find my mistake even though it has to be somewhere since I know json.load loads the json file into the dictionary and json.dump copies the dictionary to the .json file p.s the dump does work but the load returns an empty dict

I tried to read the file conventionally and then use the .loads function, did the same thing

prefix = json.load(open('file.json', 'r'))
print("Prefixes currently are:"+str(prefix)) # prints {} even though the file includes : {"551475283459309599": "!", "557678616054464512": "!", "558760765348249609": "!", "559361893861556240": "%"}

I expected it to just do what it should (load the dict or a string or something but it loads nothing)

idan b
  • 49
  • 1
  • 3
  • 1
    Are you *very* sure that the `open()` call is opening the file that you think it is opening? – BoarGules Mar 25 '19 at 17:26
  • 1
    Your code works exactly as intended in Python 3.7.2 and I highly doubt that this is different in 3.6 I think there is a bigger chance you are trying to load the wrong file. – Remy Mar 25 '19 at 17:30
  • I am sure that it is the file, I thought there might be permission problems so I even moved it to D and it didn't make a change – idan b Mar 25 '19 at 17:44
  • If there were a permission problem you would not be getting behaviour that suggests you are reading a file with an empty JSON structure. You would get "access denied". Do you maybe have your Explorer `View tab | Show/hide: File name extensions` unchecked? Then you might have a file that looks like it is called `file.json` but is actually called `file.json.txt` and another file that looks like it is called `file` but is actually called `file.json`. – BoarGules Mar 25 '19 at 20:37

2 Answers2

0

I tried running it on a different python project and it worked for some odd reason so the problem is probably somewhere else, thanks regardless!!

idan b
  • 49
  • 1
  • 3
-1

Try following the way to load json file:

import json

with open('file.json') as json_data:
    d = json.load(json_data)
    print("Prefixes currently are:"+str(d))