-1
from json import *
import pandas as pd
import numpy as np
import os

fp=open("data1.json","r")
data=json.loads(fp)
print(data)

s=pd.Series(data,index=[1,2])
print(s)

Error

enter image description here

James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

1

It has has to do with how you imported the json module. By using the * notation, you are importing everything in the json module. When importing a module in this way, you do not need to specify the module a function came from.

When importing, you have two options, import the module (and reference the module name when using it), or import everything in the module (and not reference the module name when using it).

As an aside, it is generally recommended to use the first method (import json). (reference Why is “import *” bad? and What exactly does “import *” import?).

Also note the with statement to ensure the file is properly closed when reading is finished.

# Option 1: (recommended)
import json

with open("data1.json", "w") as f:
    data = json.loads(f.read())
print(data)
# OR Option 2: (not recommended, but shown to illustrate differences in usage)

from json import *
with open("data1.json", "w") as f:
    data = loads(f.read())
print(data)
JJC
  • 80
  • 2
  • 7