【问题标题】:Problem with writing to a file message.content with emojis Discord.py使用表情符号 Discord.py 写入文件 message.content 的问题
【发布时间】:2020-02-24 04:39:08
【问题描述】:

这是我在尝试写下包含表情符号的帖子的 message.content 时遇到的错误。我怎样才能避免这种情况?

discord.ext.commands.errors.CommandInvokeError:命令引发异常:UnicodeEncodeError:“charmap”编解码器无法在位置编码字符“\U0001f609” 81:字符映射到

提前谢谢你。

这是我正在使用的代码:

async def posts(ctx):
    f = open("file.txt", "w")
    for channel in ctx.guild.text_channels:
        f.write(message.content + "\n")

    f.close()

【问题讨论】:

  • 可以显示一些代码吗?
  • 完成,抱歉忘记添加代码
  • 下次在您的问题中添加代码。使用编辑按钮。而不是创建一个新的答案...

标签: discord.py discord.py-rewrite


【解决方案1】:

问题是你没有使用 utf-8 编码。 open() 函数使用的标准编码不支持表情符号。因此错误。

要解决这个问题,您需要将encoding="utf-8" 添加到您的代码中:

async def posts(ctx):
    f = open("file.txt", "w", encoding="utf-8")
    for channel in ctx.guild.text_channels:
        f.write(message.content + "\n")

    f.close()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-24
    • 2020-07-31
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 2020-07-15
    • 2018-05-02
    • 2020-04-23
    相关资源
    最近更新 更多