Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中Kaggle API*的文档?_Python_Kaggle - Fatal编程技术网

python中Kaggle API*的文档?

python中Kaggle API*的文档?,python,kaggle,Python,Kaggle,我想编写一个python脚本,从Kaggle.com下载一个公共数据集 Kaggle API是用python编写的,但我能找到的几乎所有文档和资源都是关于如何在命令行中使用API的,很少有关于如何在python中使用Kaggle库的 一些用户似乎知道如何做到这一点,例如,请参阅,但提示不足以解决我的具体问题 也就是说,我有一个脚本如下所示: from kaggle.api.kaggle_api_extended import KaggleApi api = KaggleApi('content

我想编写一个
python
脚本,从Kaggle.com下载一个公共数据集

Kaggle API是用python编写的,但我能找到的几乎所有文档和资源都是关于如何在命令行中使用API的,很少有关于如何在
python
中使用
Kaggle
库的

一些用户似乎知道如何做到这一点,例如,请参阅,但提示不足以解决我的具体问题

也就是说,我有一个脚本如下所示:

from kaggle.api.kaggle_api_extended import KaggleApi

api = KaggleApi('content of my json metadata file')

file = api.datasets_download_file(
    owner_slug='the-owner-slug',
    dataset_slug='the-dataset-slug',
    file_name='the-file-name.csv',
)
我通过查看方法的签名得出了这个结论:
api.datasets\u下载文件(所有者\u slug,数据集\u slug,文件名,**kwargs)

我得到以下错误:

UnicodeDecodeError:'utf-8'编解码器无法解码位置12的字节0xa1:无效的开始字节

除了这个特定问题的解决方案之外,我真的很高兴知道如何使用Kaggle库排除错误,而不是检查代码本身。事实上,也许这个问题与utf编码无关,但我不知道如何解决这个问题。如果只是文件名错了,或者像这样愚蠢的事情怎么办


csv文件没有什么特别之处:有三列,第一列是timestamp,另外两列是整数。

您可以检查

但是如果你懒惰,你可以在你的服务器上安装
kaggle
pip安装kaggle

要下载整个竞赛,您可以从python中调用它

import os
os.system('kaggle competitions download -c "dogs-vs-cats-redux-kernels-edition"')
此外,请查看我发布的一篇文章,其中解释了竞争、数据集和内核交互的大多数常见用例

下面是使用Python中的Kaggle API所涉及的步骤

设置API密钥

转到您的Kaggle帐户选项卡
https://www.kaggle.com//account
然后单击“创建API令牌”。将下载名为kaggle.json的文件。将此文件移入Mac和Linux中的~/.kaggle/文件夹或windows中的C:\Users\.kaggle\文件夹

或者,您可以使用KAGGLE.json中的值填充KAGGLE_USERNAME和KAGGLE_KEY环境变量,以使api进行身份验证

使用API服务器进行身份验证

 from kaggle.api.kaggle_api_extended import KaggleApi
 api = KaggleApi()
 api.authenticate()
下载数据集

# Download all files of a dataset
# Signature: dataset_download_files(dataset, path=None, force=False, quiet=True, unzip=False)
api.dataset_download_files('avenn98/world-of-warcraft-demographics')

# downoad single file
#Signature: dataset_download_file(dataset, file_name, path=None, force=False, quiet=True)
api.dataset_download_file('avenn98/world-of-warcraft-demographics','WoW Demographics.csv')
# Download all files for a competition
# Signature: competition_download_files(competition, path=None, force=False, quiet=True)
api.competition_download_files('titanic')

# Download single file for a competition
# Signature: competition_download_file(competition, file_name, path=None, force=False, quiet=False)
api.competition_download_file('titanic','gender_submission.csv')
# Signature: competition_submit(file_name, message, competition,quiet=False)
api.competition_submit('gender_submission.csv','API Submission','titanic')
下载比赛文件

# Download all files of a dataset
# Signature: dataset_download_files(dataset, path=None, force=False, quiet=True, unzip=False)
api.dataset_download_files('avenn98/world-of-warcraft-demographics')

# downoad single file
#Signature: dataset_download_file(dataset, file_name, path=None, force=False, quiet=True)
api.dataset_download_file('avenn98/world-of-warcraft-demographics','WoW Demographics.csv')
# Download all files for a competition
# Signature: competition_download_files(competition, path=None, force=False, quiet=True)
api.competition_download_files('titanic')

# Download single file for a competition
# Signature: competition_download_file(competition, file_name, path=None, force=False, quiet=False)
api.competition_download_file('titanic','gender_submission.csv')
# Signature: competition_submit(file_name, message, competition,quiet=False)
api.competition_submit('gender_submission.csv','API Submission','titanic')
参加比赛

# Download all files of a dataset
# Signature: dataset_download_files(dataset, path=None, force=False, quiet=True, unzip=False)
api.dataset_download_files('avenn98/world-of-warcraft-demographics')

# downoad single file
#Signature: dataset_download_file(dataset, file_name, path=None, force=False, quiet=True)
api.dataset_download_file('avenn98/world-of-warcraft-demographics','WoW Demographics.csv')
# Download all files for a competition
# Signature: competition_download_files(competition, path=None, force=False, quiet=True)
api.competition_download_files('titanic')

# Download single file for a competition
# Signature: competition_download_file(competition, file_name, path=None, force=False, quiet=False)
api.competition_download_file('titanic','gender_submission.csv')
# Signature: competition_submit(file_name, message, competition,quiet=False)
api.competition_submit('gender_submission.csv','API Submission','titanic')
检索领导委员会

# Signature: competition_view_leaderboard(id, **kwargs)
leaderboard = api.competition_view_leaderboard('titanic')

您是否尝试过使用Kaggle CLI?()您已经可以下载数据集了。这是可以用来下载文件的命令:
kg download-u-p-c
这不是我要问的。我想了解
Kaggle
作为
python
库在
python
脚本中的应用。谢谢,我知道这一切。现在我使用的是调用
os.system
的“懒惰”解决方案,但考虑到
kaggle
本身就是用
python
编写的,这简直是荒谬可笑。你的评论没有告诉我任何我不知道或没有尝试过的事情,所以我想这次没有投票权。所以确实如此,到目前为止,我所能找到的最好的资源是api的实际来源:谢谢@Jose CherianI面临一个问题