且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Python中替换字符串中的无效unicode字符?

更新时间:2023-11-15 16:22:52

感谢您的评论。这样,我就能实施更好的解决方案:

Thanks to you for your comments. This way I was able to implement a better solution:

    try:
        s2 = codecs.encode(s, "utf-8")
        return (True, s, None)
    except Exception as e:
        ret = codecs.decode(codecs.encode(s, "utf-8", "replace"), "utf-8")
        return (False, ret, e)

请分享对该解决方案的任何改进。谢谢!

Please share any improvements on that solution. Thank you!