Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
json.load() in Python 3
#1
Hi

I'm trying to adapt some code I've found online for use in my own function. I can get the code to run in Python 2 but would really like to get it to work in Python 3.

The code that works in Python 2 is:
for ID in id_list:
        try:
                with open(location + ID+ '.json','r') as f:
                        data = json.load(f)
To run in Python 3, I've tried changing it to (the files are utf-8 encoded):
for ID in id_list:
        try:
                with open(location + ID+ '.json',encoding = 'utf-8') as f:
                        data = json.load(f)
This worked for another occasion where the .json files had ANSI encoding but here I get the error:
Error:
Traceback (most recent call last): File "<ipython-input-24-e1396ed76a2e>", line 1, in <module> runfile('...', wdir='...') File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace) File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "...", line 166, in <module> X2 = function(X) File "...", line 110, in Function data = json.load(f) File "C:\ProgramData\Anaconda3\lib\json\__init__.py", line 296, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) File "C:\ProgramData\Anaconda3\lib\json\__init__.py", line 348, in loads return _default_decoder.decode(s) File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None JSONDecodeError: Expecting value
Any help why this isn't working?

Thanks!
Reply
#2
try (using f-string (if your python 3.6 or newer)):
for id in id_list:
    with open(f'location{id}') as fp:
        data = json.load(fp)
if using python 3 older than 3.6 use:
for id in id_list:
    with open(f'location{}'.format(id)) as fp:
        data = json.load(fp)
FYI: please use 4 space indentation
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failed attempts to load Microsoft Appstore Python DLLs piyushd 0 394 Oct-31-2023, 10:43 AM
Last Post: piyushd
  JSON Dump and JSON Load foxholenoob 8 975 Oct-12-2023, 07:21 AM
Last Post: foxholenoob
  Python Split json into separate json based on node value CzarR 1 5,474 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  How to load log.txt directly into python codes? sparkt 6 2,899 Aug-21-2020, 03:51 PM
Last Post: sparkt
  Load table from Oracle to MYSQL using python himupant94 0 1,608 May-12-2020, 04:50 PM
Last Post: himupant94
  Python - help with getting JSON from one DB and load to another DB qIekm 4 3,177 Apr-16-2020, 07:07 AM
Last Post: qIekm
  difficulties to chage json data structure using json module in python Sibdar 1 2,043 Apr-03-2020, 06:47 PM
Last Post: micseydel
  problem with mapnik in anaconda python 2: from _mapnik import * ImportError: DLL load parsley 0 1,873 Dec-11-2019, 07:50 AM
Last Post: parsley
  python and load balancer pythonFresher 1 2,245 Jul-18-2019, 07:23 AM
Last Post: pythonFresher
  Load JSON file data into mongodb using pymongo klllmmm 1 11,801 Jun-28-2019, 12:47 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

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