[英]How can i decode and encode this character in python using utf-8?
[英]I want tkinter asksaveasfile() to save as utf-8 in python. How can I solve it?
我在 Python 中编写了以下代码:
sveas = f.asksaveasfile()
sveas.write(f'{code.get(1.0,END)}')
sveas.close()
在这里,我希望它用 utf-8 对文件进行编码。 我该如何解决
如果要指定编码类型,需要在打开文件时传入encoding='utf-8'
。 由于asksaveasfile()
将为您隐式打开文件,但您无法指定编码类型,因此需要使用asksaveasfilename()
,然后使用open()
显式打开所选文件:
filename = f.asksaveasfilename()
with open(filename, 'w', encoding='utf-8') as fh:
fh.write(code.get('1.0', END))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.