gpt4 book ai didi

python - 写入文件时更改编码

转载 作者:行者123 更新时间:2023-11-30 23:24:09 25 4
gpt4 key购买 nike

我正在编写一个文件:

 with open("test_%s.tem" %i, "w") as f1:
....
f1.close()

我想将编码转换为 UCS-2 小端格式。是否可以在 open() 或 close() 函数中执行此操作。任何想法都会有帮助。

最佳答案

Python 3中,根据文档:https://docs.python.org/3/library/functions.html#open
您可以使用encoding参数指定编码。

with open("test_%s.tem" %i, "w", encoding="utf16") as f1:

关于python - 写入文件时更改编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23605517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com