【问题标题】:How can I load this json file successfully?如何成功加载此 json 文件?
【发布时间】:2020-04-15 17:55:06
【问题描述】:

我正在构建一个不和谐的机器人,它将与人类打牌。问题是当我尝试加载 JSON 文件时,程序不起作用。

`@bot.command(pass_context=True)
async def loadCards(ctx):
  with open('wcards.json') as f:
    wtcards = json.load(f)
  with open('bcards.json') as f:
    bkcards = json.load(f)
  if len(wtcards) > 1 and len(bkcards) > 1:
    await ctx.send('Cards Loaded')`

【问题讨论】:

  • 你能比“它不起作用”更具体吗?你有错误吗?输出是什么?
  • 它说'str'对象没有属性'read'

标签: python-3.7 discord.py


【解决方案1】:

相信你在使用open()的时候需要用到read和write参数。 例如

@bot.command(pass_context=True)
async def loadCards(ctx):
  with open('wcards.json', 'r') as f:
    wtcards = json.load(f)
  with open('bcards.json', 'r') as f:
    bkcards = json.load(f)
  if len(wtcards) > 1 and len(bkcards) > 1:
    await ctx.send('Cards Loaded')

【讨论】:

    猜你喜欢
    • 2019-08-20
    • 2021-09-12
    • 1970-01-01
    • 2012-02-07
    • 2017-05-08
    • 2016-05-25
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多