TypeError: Object of type TextIOWrapper is not JSON serializable
TypeError: Object of type TextIOWrapper is not JSON serializable
如果代码能够正常工作,那么每当有人在聊天中输入内容时,他们就会获得 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
您使用的是 loads
and dumps
backwards, and you should be using load
and dump
(s
后缀表示这些函数适用于字符串。)。 load
从一个文件,dump
到一个文件
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
错误的方法:
正确方法:
with open("Jello.json", "w") as json_file:
json.dump(object_to_be_saved, json_file)
错误的方式:
我用另一种方式做到了 json.dump(json_file, object_to_be_saved)...谢谢 Python.
如果代码能够正常工作,那么每当有人在聊天中输入内容时,他们就会获得 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
您使用的是 loads
and dumps
backwards, and you should be using load
and dump
(s
后缀表示这些函数适用于字符串。)。 load
从一个文件,dump
到一个文件
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
错误的方法:
正确方法:
with open("Jello.json", "w") as json_file:
json.dump(object_to_be_saved, json_file)
错误的方式:
我用另一种方式做到了 json.dump(json_file, object_to_be_saved)...谢谢 Python.