【问题标题】:Traceback when using Pandas to convert to Json Format使用 Pandas 转换为 Json 格式时的回溯
【发布时间】: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


【解决方案1】:

您得到这个是因为filename 没有扩展名。 Pandas 将使用文件名的扩展名(如xlsxxls)(如果你没有给它一个ExcelWriter)来理解用于这种格式的正确库。只需尝试使用 df.to_excel('filename.xlsx', index=False) 之类的东西,看看效果如何。

【讨论】:

  • 搞定了:'filename = input() + '.xlsx'
猜你喜欢
  • 2017-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 2022-07-22
  • 2014-01-05
  • 2021-02-24
相关资源
最近更新 更多