简体   繁体   中英

How to import `json` package in python3.6

I am using python3.6. Below code is to use json module to parse data:

import urllib.request
from pandas import *
from pandas import json
import numpy as np
import pandas as pd
import requests
import urllib
import time
def request_image_info(lat, lon):
    res = urllib.request.urlopen('https://biocache.ala.org.au/ws/explore/group/Plants.json?lat='+str(lat)+'&lon='+str(lon)+'&pageSize=1&radius=1')
    json_data = json.load(res)
    guid = json_data[0]['guid']
    name = json_data[0]['name']
    family = json_data[0]['family']
    common_name = json_data[0]['commonName']
    count = json_data[0]['count']
    rank = json_data[0]['rank']
    kingdom = json_data[0]['kingdom']
    image_id = time.time()

I got below error when run this code. The line19 is the code json_data = json.load(res)

Traceback (most recent call last):
  File "loadImages.py", line 57, in <module>
    request_image_info(row[2], row[3])
  File "loadImages.py", line 19, in request_image_info
    json_data = json.load(res)
  File "/Users/admin/tmp/p36/lib/python3.6/site-packages/pandas/util/_depr_module.py", line 61, in __getattr__
    obj = getattr(deprmodule, name)
AttributeError: module 'pandas.json' has no attribute 'load'

I have installed pandas module as below:

$ pip3 install pandas
Requirement already satisfied: pandas in /usr/local/lib/python3.6/site-packages
Requirement already satisfied: numpy>=1.7.0 in /usr/local/lib/python3.6/site-packages (from pandas)
Requirement already satisfied: pytz>=2011k in /usr/local/lib/python3.6/site-packages (from pandas)
Requirement already satisfied: python-dateutil>=2 in /usr/local/lib/python3.6/site-packages (from pandas)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/site-packages (from python-dateutil>=2->pandas)

how can I solve this issue?

Below is the python and pip version I am using:

$ python --version
Python 3.6.2
$ pip --version
pip 9.0.1 from /Users/admin/tmp/p36/lib/python3.6/site-packages (python 3.6)

You're importing wrong json . Remove

from pandas import json

and do

import json

instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM