gpt4 book ai didi

python - 如何用印地语将数据写入文件?

转载 作者:太空宇宙 更新时间:2023-11-03 21:05:10 25 4
gpt4 key购买 nike

我正在尝试使用 python tkinter 将数据写入非罗马脚本中的文件。我想将数据写入遵循梵文脚本的印地语文件。然而,当我用英语写入一些数据时,它工作得很好,而用印地语写入文件时会抛出一些错误。我该如何解决这个问题?

我尝试在 filedialog 命令中添加 (encoding='utf-8') 。这样做会出现以下错误:

_tkinter.TclError:错误选项“-encoding”:必须是 -confirmoverwrite、-defaultextension、-filetypes、-initialdir、-initialfile、-parent、-title 或 -typevariable

当我单击按钮时,就会出现此错误。也许 (encoding='utf-8') 不是 filedialog 的属性。以下是代码的相关部分:

def save_file_hindi(event=""):
data = filedialog.asksaveasfile(mode="w", defaultextension=".html")
if data is None:
return

data.write("एक" + "<br>\n"+ "दो" + "<br>\n")
data.close()

预期结果:在保存的 HTML 文件中,应该显示:

एक

दो

实际结果:UnicodeEncodeError:“charmap”编解码器无法对位置 19-26 中的字符进行编码:字符映射到未定义

最佳答案

尝试了这段代码,它在 Windows 10 下的 Python 3.6.5 上运行得很好:

text = """<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head><body>
<p>एक दो</p>
</body></html>"""

with open('test.html', 'w', encoding='utf8') as file:
file.write(text)

你的环境是什么?您是否尝试过在不同的编辑器和/或浏览器中打开该文件?

关于python - 如何用印地语将数据写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55459768/

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