美文网首页
python3报'encoding' is an invalid

python3报'encoding' is an invalid

作者: CoderZb | 来源:发表于2022-04-25 15:35 被阅读0次

执行output_json_file 功能函数时候,print 打印出'encoding' is an invalid keyword argument for this function的错误

错误代码

def output_json_file(jsonData, jsonFile):
    try:
        with open(jsonFile, 'w', encoding = 'utf-8') as f:
            f.write(jsonData.decode('utf8'))
        return jsonFile
    except Exception as e:
        print "error is: ", e
        return

正确代码

with open改为with io.open,并引入import io

import io
def output_json_file(jsonData, jsonFile):
    try:
        with io.open(jsonFile, 'w', encoding = 'utf-8') as f:
            f.write(jsonData.decode('utf8'))
        return jsonFile
    except Exception as e:
        print "error is: ", e
        return

相关文章

网友评论

      本文标题:python3报'encoding' is an invalid

      本文链接:https://www.haomeiwen.com/subject/xvgfyrtx.html