Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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 TypeError:TextIOWrapper类型的对象不可JSON序列化_Python_Json_Discord_Discord.py_Python 3.7 - Fatal编程技术网

Python TypeError:TextIOWrapper类型的对象不可JSON序列化

Python TypeError:TextIOWrapper类型的对象不可JSON序列化,python,json,discord,discord.py,python-3.7,Python,Json,Discord,Discord.py,Python 3.7,如果代码能够正常工作,那么每当有人在聊天中键入内容时,他们就会获得5次体验,并且这些信息会被放入.json文件中,但相反,每当有人在聊天中键入内容时,就会出现此错误 on_message users = json.dumps(f) TypeError: Object of type TextIOWrapper is not JSON serializable 以下是我正在使用的代码: import discord from discord.ext import commands from d

如果代码能够正常工作,那么每当有人在聊天中键入内容时,他们就会获得5次体验,并且这些信息会被放入
.json
文件中,但相反,每当有人在聊天中键入内容时,就会出现此错误

on_message users = json.dumps(f) 
TypeError: Object of type TextIOWrapper is not JSON serializable
以下是我正在使用的代码:

import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
import json
from json import dumps, loads, JSONEncoder, JSONDecoder
import os

client = commands.Bot(command_prefix='^')
os.chdir(r'C:\Users\quiny\Desktop\sauce')

@client.event
async def on_ready():
    print ("Ready when you are xd")
    print ("I am running on " + client.user.name)
    print ("With the ID: " + client.user.id)

@client.event
async def on_member_join(member):
    with open('users.json', 'r') as f: 
        users = json.dumps(f)

    await update_data(users, member)

    with open('users.json', 'w') as f:
        json.loads("users, f")

@client.event
async def on_message(message):
    with open('users.json', 'r') as f:
        users = json.dumps(f)

    await update_data(users, message.author)
    await add_experience(users, message.author, 5)
    await level_up(users, message.author, message.channel)

    with open('users.json', 'w') as f:
        json.loads("users, f")

async def update_data(users, user):
    if not user.id in users:
        users[user.id] = {}
        users[user.id]['experience'] = 0
        users[user.id]['level'] = 1

async def add_experience(users, user, exp):
    users[user.id]['experience'] += exp

async def level_up(users, user, channel):
    experience = users[user.id]['experience']
    lvl_start = users[user.id]['level']
    lvl_end = int(experience ** (1/4))

    if lvl_start < lvl_end:
        await client.send_message(channel, '{} has achieved a slightly higher 
level of {}, yay'.format(user.mention, lvl_end))
        users[user.id]['level'] = lvl_end
导入不一致
从discord.ext导入命令
从discord.ext.commands导入Bot
导入异步
导入json
从json导入转储、加载、JSONECODER、JSONDecoder
导入操作系统
client=commands.Bot(命令前缀=“^”)
chdir(r'C:\Users\quiny\Desktop\sauce')
@客户端事件
_ready()上的异步定义:
打印(“xd时准备就绪”)
打印(“我正在”+client.user.name上运行)
打印(“ID为:+client.user.ID)
@客户端事件
成员加入时的异步定义(成员):
将open('users.json','r')作为f:
users=json.dumps(f)
等待更新_数据(用户、成员)
将open('users.json','w')作为f:
加载(“用户,f”)
@客户端事件
异步def on_消息(消息):
将open('users.json','r')作为f:
users=json.dumps(f)
等待更新_数据(用户,message.author)
等待添加体验(用户,message.author,5)
等待级别(用户、message.author、message.channel)
将open('users.json','w')作为f:
加载(“用户,f”)
异步def更新_数据(用户,用户):
如果不是用户中的user.id:
用户[user.id]={}
用户[user.id]['experience']=0
用户[user.id]['level']=1
异步def添加体验(用户、用户、经验):
用户[user.id]['experience']+=exp
异步def液位(用户、用户、通道):
体验=用户[user.id]['experience']
lvl_start=users[user.id]['level']
第二层=内部(经验**(1/4))
如果lvl_开始
您有自己的and向后,您应该使用and代替(
s
后缀表示那些函数在字符串上工作。)<代码>从文件加载
转储
到文件

users = {}

@client.event
async def on_message(message):
    # No need to load the dictionary, our copy is the most correct
    await update_data(users, message.author)
    await add_experience(users, message.author, 5)
    await level_up(users, message.author, message.channel)
    with open('users.json', 'w') as f:
        json.dump(users, f)

@client.event
async def on_ready():
    print ("Ready when you are xd")
    print ("I am running on " + client.user.name)
    print ("With the ID: " + client.user.id)
    # Load the json just once, when the bot starts
    global users
    with open('users.json') as f:
        try:
            users = json.load(f)
        except:
            users = {}

我有这个错误,我使用了
json.dump
错误的方法:

正确的方法:
打开(“Jello.json”,“w”)作为json\u文件:
dump(要保存的对象,json文件)
错误的方式:
我在json.dump(json_文件,object_to_be_saved)中使用了另一种方法。。。谢谢Python。

FWIW:我得到了这个错误,因为我不小心将参数交换到了
json.dump()
,例如,我使用了
json.dump(fp,obj)
而不是正确的
json.dump(obj,fp)
。如果您将问题减少到最小的失败示例,这个问题将会得到改进。您有许多与
json
库无关的用于异步、事件处理等的额外代码。如果你把这个问题整理一下,我就+1这个问题。