2017-01-26 6 views
-1

以下の複数のURLコードからjsonデータを抽出しようとしていますが、タイトルに記載されているエラーが発生します。 しかし、URLを1つしか使用しない場合、コードは機能します。AttributeError: 'list'オブジェクトに 'loads'属性がありません

何か助けていただければ幸いです。

import urllib, json, time, csv 

arr_ids = ['611', '1564', '1565', '1561', '712', '779', '118', '707', '706', '711', '155', '713', '710', '607', '609', '592', '739', '589', '608', '606', '569', '570', '612', '587', '567', '591', '564', '563', '566', '565', '568', '588', '1561', '1387', '1388', '1575', '1567', '1577', '1568', '152', '154', '153', '1203', '1204', '708', '709', '1576', '780', '781', '1573', '1574', '782', '121', '120', '1562', '1385', '1386', '1563'] 
convert_list = [['date_and_time','val']] 

for arr_id in arr_ids: 
     url = "http://xxxyyyyx.com/predict/dataloc.php?param=rv&dfrm=01/24/2017&dto=01/25/2017&numloc=1&data24=0&locs[]="+arr_id 
     response = urllib.urlopen(url) 
     data = json.loads(response.read()) 
     for key in data: 
      station_name = key 
      json_data = data[station_name] 
      for json in json_data: 
        epoch_time = json[0]/1000 
        formatted = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(epoch_time)) 
        val = json[1] 
        new_list = [formatted,val] 
        convert_list.append(new_list) 

      with open(station_name+".csv", "wb") as f: 
       writer = csv.writer(f) 
       writer.writerows(convert_list) 

スローされますエラー:この行で

Traceback (most recent call last): File "C:\Users\acer\Downloads\getwl_rv\json2csv\json2csv.py", line 9, in data = json.loads(response.read()) AttributeError: 'list' object has no attribute 'loads'

答えて

3

for json in json_data:

、あなたはjsonモジュール名を上書きしてしまいました。あなたは

for json in json_data: 

をインポートされたモジュールを上書きしてしまった

+0

何...愚かな間違い。ありがとう! –

2

はモジュールによって、あなたの変数に名前を付けないようにしてくださいまたは機能に建て

関連する問題