skip to Main Content

I am very fresh in Python. I would like to read JSON files in Python, but I did not get what are the problems. Please see the image.

enter image description here

3

Answers


  1. You have to specify a mode to the open() function. In this case I think you’re trying to read the file, so your mode would be "r". Your code should be:

    with open(r'path/to/read/','r') as file: 
       data = json.load(file)
    

    Your code should run now.

    Login or Signup to reply.
  2. Your path should not contain spaces. Please modify the file path.

    Generally speaking, the file path is best to be in full English with no spaces and no special characters.

    enter image description here

    Login or Signup to reply.
  3. import sys
    import os
    import json
    
    def JsonRead(str):
        with open(str, encoding='utf-8') as f:
            data = json.load(f)
        return data
    
    new_Data = JsonRead(filePath)
    

    Then import JsonRead in project

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search