gpt4 book ai didi

Python 程序在 Eclipse 中工作,但当我直接运行它时不工作(Unicode 东西)

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

我搜索并发现了一些相关问题,但它们处理 Unicode 的方式不同,所以我无法将解决方案应用到我的问题。

我不会粘贴我的整个代码,但我确信这个孤立的示例代码复制了错误:(我也在使用 wx 作为 GUI,所以这就像在一个类中一样)

#coding: utf-8
...
something = u'ЧЕТЫРЕ'
//show the Russian text in a Label on the GUI
self.ExampleLabel.SetValue(str(self.something))

在 Eclipse 上一切正常,并且显示俄语字符。但是,当我尝试直接通过文件打开 Python 时,我在 CL 上收到此错误:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-11: 
ordinal not in range(128)

我认为这与 CL 无法输出 Unicode 字符和 Eclipse 执行幕后魔术有关。关于如何使其独立运行的任何帮助?

最佳答案

当您在未指定编码的情况下调用 str() 时,将使用默认编码,这取决于您的程序运行的环境。在 Eclipse 中,这与命令行不同。

不要依赖默认编码,而是明确指定:

self.ExampleLabel.SetValue(self.something.encode('utf-8'))

您可能想研究 Python Unicode HOWTO了解编码和 str() 对 unicode 对象的作用。 wxPython 项目有一个 page on Unicode usage

关于Python 程序在 Eclipse 中工作,但当我直接运行它时不工作(Unicode 东西),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12234690/

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