gpt4 book ai didi

Python数据输出到Excel

转载 作者:行者123 更新时间:2023-12-02 05:42:08 24 4
gpt4 key购买 nike

在花费数小时尝试找出将统计数据导入 Excel 文件的方法时,我遇到了我尝试在 Python 中使用的此脚本的一个版本。执行时,我在脚本的第二个 csv_output 部分下方收到以下错误:

KeyError: 0

我刚刚开始学习 Python 的细微差别,无法真正弄清楚我在这里做错了什么。

我目前正在使用 Python 3.6 和 Windows 10。如有任何帮助,我们将不胜感激。

import requests
import csv

url = "http://stats.nba.com/stats/leagueLeaders?
LeagueID=00&PerMode=PerGame&Scope=S&Season=2017-
18&SeasonType=Regular+Season&StatCategory=PTS"

data = requests.get(url, timeout=5)
entries = data.json()

with open('output.csv', 'w') as f_output:
csv_output = csv.writer(f_output)
csv_output.writerow(entries['resultSet'][0]['headers'])
csv_output.writerows(entries['resultSet'][0]['rowSet'])

最佳答案

  1. 我没有看到名为 requests 的模块。也许尝试 urllib.request.urlopen(url, timeout=5)

  2. URL 不正确。如果您只是使用浏览器打开,则会导致错误。也许再次检查您的网址。

关于Python数据输出到Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47086615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com