python混账的编码问题解决之道

时间:2022-04-23
本文章向大家介绍python混账的编码问题解决之道,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

下面的代码作用是修改文件的编码格式。代码很简单,但是也很牛逼(在我看来),这是在segment上找到的解决办法,废话不多说,直接上代码。

import codecs

def ReadFile(filePath, encoding):
    with codecs.open(filePath, "r", encoding=encoding) as f:
    return f.read()

def WriteFile(filePath, content, encoding):
    with codecs.open(filePath, "w", encoding=encoding) as f:
    f.write(content)

def UTF8_to_GBK(src, dst):
    content = ReadFile(src, encoding="gbk")
    WriteFile(dst, content, "utf-8")