【发布时间】:2020-09-29 13:01:21
【问题描述】:
我最近一直在尝试使用 NBA API 来提取投篮图表数据。我将链接我正在使用的特定功能的文档here。
我不断得到如下回溯:
Traceback (most recent call last):
File "nbastatsrecieve2.py", line 27, in <module>
df.to_excel(filename, index=False)
File "C:\Users\*\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\core\generic.py", line 2023, in to_excel
formatter.write(
File "C:\Users\*\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\io\formats\excel.py", line 730, in write
writer = ExcelWriter(stringify_path(writer), engine=engine)
File "C:\Users\*\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\io\excel\_base.py", line 637, in __new__
raise ValueError(f"No engine for filetype: '{ext}'") from err
ValueError: No engine for filetype: ''
这是我目前拥有的所有代码:
from nba_api.stats.endpoints import shotchartdetail
import pandas as pd
import json
print('Player ID?')
playerid = input()
print('File Name?')
filename = input()
response = shotchartdetail.ShotChartDetail(
team_id= 0,
player_id= playerid
)
content = json.loads(response.get_json())
# transform contents into dataframe
results = content['resultSets'][0]
headers = results['headers']
rows = results['rowSet']
df = pd.DataFrame(rows)
df.columns = headers
# write to excel file
df.to_excel(filename, index=False)
希望有人可以提供帮助,因为我对 JSON 格式非常陌生。
【问题讨论】:
-
您的代码对我来说运行良好。只要确保您安装了最新的
openpyxl。并且文件名应该有一个 .xlsx 扩展名。 -
@NYCCoder 我应该如何更新openpyxl,因为现在说找不到模块
-
pip install openpyxl --upgrade -
@NYCCoder 感谢您的帮助!现在我需要弄清楚为什么它只返回给我的镜头。
标签: python json pandas nba-api