2017-05-07 5 views
1

練習として、私はAPIからデータを取り出してpsqlデータベースに挿入しています。私は当初、プル当たり1000エントリのデフォルト制限に従っていましたが、私は約40K行のデータをすべて取得しようとしました。実験のビットの後、私は4800を引くことができますが、その後、私は次の取得:私が見てきたPython KeyError:<peewee.IntegerField object ...> peeweeを使用していますinsert_many()

import peewee 


psql_db = peewee.PostgresqlDatabase('database', user='my_username') 

class Bike_Count(peewee.Model): 
    date = peewee.DateTimeField() 
    fremont_bridge_sb = peewee.IntegerField() 
    fremont_bridge_nb = peewee.IntegerField() 

    class Meta: 
     database = psql_db 

Traceback (most recent call last): 
    File "data_pull.py", line 19, in <module> 
    postgres_db.Bike_Count.insert_many(data).execute() 
    File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 3516, in execute 
    cursor = self._execute() 
    File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 2901, in _execute 
    sql, params = self.sql() 
    File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 3484, in sql 
    return self.compiler().generate_insert(self) 
    File "/usr/local/lib/python3.5/dist-packages/peewee.py", line 2084, in generate_insert 
    value = row_dict[field] 
KeyError: <peewee.IntegerField object at 0x7f5b32c2c7f0> 

data_pull.py

import json, requests, peewee 
import postgres_db 


endpoint = 'https://data.seattle.gov/resource/4xy5-26gy.json?$limit=4800' 

response = requests.get(endpoint, headers={'X-App-Token': '(REMOVED)'}) 
if response.status_code == 200: 
    data = json.loads(response.text) 


postgres_db.Bike_Count.create_table(True) 
postgres_db.Bike_Count.insert_many(data).execute() 

postgres_db.pyオンラインのテーブルではそこにエントリーの問題があったと思っていましたが、私は明らかなものは何も見つかりません。助けてくれてありがとう。私は(トークンと4800リミットアプリを削除)ローカルであなたのコードを試してみましたが、それが期待通りに働いていた

+0

レスポンスデータ文字列を必要に応じてフォーマットせずに直接テーブルに挿入する方法はありますか?応答はdatetime値と2つの整数のリストを返すことが保証されていますか? – wave5459

+0

必要に応じてフォーマットするとどういう意味ですか?私の理解は、json.loads()がオブジェクトを返すことです。この場合は辞書のリストです。 APIドキュメントでは、フィールドは日付/時刻フィールドと2つの数値フィールドであると言います。 〜4800行までのテーブルを取得できてエラーが発生するのは単なる奇妙なことです。 –

+0

データセットに関する情報へのリンクは次のとおりです。https://data.seattle.gov/Transportation/Fremont-Bridge-Hourly-Bicycle-Counts-by-Month-Octo/65db-xm6k –

答えて

1

:私は添付LIMITでそれを実行したときに、私は気づいた何

id |  date   | fremont_bridge_sb | fremont_bridge_nb 
------+---------------------+-------------------+------------------- 
    1 | 2017-01-09 06:00:00 |    28 |    55 
    2 | 2017-01-04 20:00:00 |    19 |    10 
    3 | 2017-01-18 13:00:00 |    18 |    18 
    4 | 2017-01-06 11:00:00 |    22 |    15 
    5 | 2017-01-27 11:00:00 |    39 |    38 
    6 | 2017-01-08 14:00:00 |     6 |    10 
    7 | 2017-01-06 23:00:00 |     8 |     3 
    8 | 2017-01-27 13:00:00 |    45 |    35 
... 

は、行の1つが返されるということですAPIには、dateキー(fremont_bridge_nbとfremont_bridge_sbフィールドがありません)が含まれています。

ピーウィーは、各行が同じキーを持つバルク挿入のために必要とするので、問題は、ピーウィーはすべて3つのキーを見つけることが期待されます。

+0

そのようなデータ。私は昨日それに気づいた。実際には、欠損値である行が少なくとも2つあります。私は ''ヌル= True'''を設定して、ヌルであれば大丈夫だと言ってみましたが、トラブルシューティングをしたあと、デフォルト値を1に設定しました。 –

関連する問題