2017-04-20 6 views
2

私はcsv fileKaggleにSpaceXミッションを直接インポートするためにさまざまな方法を試してきましたが、成功することはありません。ダウンロードURLからpandas DataFrameにインポートするkaggle csv

私はログイン要求を送信する必要があります。これは私が今までに持っているものです:

import requests 
import pandas as pd 
from io import StringIO 

# Link to the Kaggle data set & name of zip file 
login_url = 'http://www.kaggle.com/account/login?ReturnUrl=/spacex/spacex-missions/downloads/database.csv' 

# Kaggle Username and Password 
kaggle_info = {'UserName': "user", 'Password': "pwd"} 

# Login to Kaggle and retrieve the data. 
r = requests.post(login_url, data=kaggle_info, stream=True) 
df = pd.read_csv(StringIO(r.text)) 

rはページのhtmlコンテンツを返しています。 df = pd.read_csv(url)はCParserエラーを与える: CParserError: Error tokenizing data. C error: Expected 1 fields in line 13, saw 6

私は解決策を探してきましたが、私が試したこれまでのところ、何も働きました。

答えて

0

ストリームを作成し、それを直接パンダに渡します。私はあなたがパンダにオブジェクトのようなファイルを渡す必要があると思います。可能な解決策についてはthis answerを見てください(投稿を使用し、リクエストには入りません)。

また、あなたが使用しているリダイレクトのログインURLはそのままではないと思います。 I know i suggested that here。しかし、ポストリクエストコールがリダイレクト(私は疑わしい)を処理しなかったので、私は使用していませんでした。

def from_kaggle(data_sets, competition): 
    """Fetches data from Kaggle 

    Parameters 
    ---------- 
    data_sets : (array) 
     list of dataset filenames on kaggle. (e.g. train.csv.zip) 

    competition : (string) 
     name of kaggle competition as it appears in url 
     (e.g. 'rossmann-store-sales') 

    """ 
    kaggle_dataset_url = "https://www.kaggle.com/c/{}/download/".format(competition) 

    KAGGLE_INFO = {'UserName': config.kaggle_username, 
        'Password': config.kaggle_password} 

    for data_set in data_sets: 
     data_url = path.join(kaggle_dataset_url, data_set) 
     data_output = path.join(config.raw_data_dir, data_set) 
     # Attempts to download the CSV file. Gets rejected because we are not logged in. 
     r = requests.get(data_url) 
     # Login to Kaggle and retrieve the data. 
     r = requests.post(r.url, data=KAGGLE_INFO, stream=True) 
     # Writes the data to a local file one chunk at a time. 
     with open(data_output, 'wb') as f: 
      # Reads 512KB at a time into memory 
      for chunk in r.iter_content(chunk_size=(512 * 1024)): 
       if chunk: # filter out keep-alive new chunks 
        f.write(chunk) 

使用例:

sets = ['train.csv.zip', 
     'test.csv.zip', 
     'store.csv.zip', 
     'sample_submission.csv.zip',] 
from_kaggle(sets, 'rossmann-store-sales') 

あなたはファイルを解凍する必要があるかもしれません

私は私のプロジェクトで使用して終了コードは、このでした。

def _unzip_folder(destination): 
    """Unzip without regards to the folder structure. 

    Parameters 
    ---------- 
    destination : (str) 
     Local path and filename where file is should be stored. 
    """ 
    with zipfile.ZipFile(destination, "r") as z: 
     z.extractall(config.raw_data_dir) 

私は本当に直接DataFrameに読み込まず、まずディスクに格納しました。しかし、tempディレクトリを使用するように変更し、ファイルを読み込んだ後にファイルを削除するだけで済みます。

+0

Actaully私はパンダがzipedの間にcsvファイルを読むことができると思います。 http://stackoverflow.com/questions/18885175/read-a-zipped-file-as-a-pandas-dataframe –

関連する問題