我有一个简单的python / pyqt应用程序,它将数据插入到SQLITE数据库表中, 一些条目包含特殊字符,如变音符号。 我使用eclipse进行了开发,测试了它并设法插入所有数据而没有任何错误。 然后我决定创建一个我的应用程序的可执行文件。我正在使用py2exe来创建可执行文件。我的setup.py看起来如下
from distutils.core import setup
import py2exe
import os
includes=["sqlite3","sip","PyQt4", "PyQt4.QtGui"]
excludes=[]
packages=[]
dll_excludes=['libgdk-win32-1.0-0.dll', 'libgobject-2.0-0.dll', 'tcl84.dll', 'tk84.dll']
setup(
options={"py2exe": {"compressed": 2,
"optimize": 2,
"includes": includes,
"excludes": excludes,
"packages": packages,
"dll_excludes": dll_excludes,
"bundle_files": 2,
"dist_dir": "dist",
"xref": False,
"skip_archive": False,
"ascii": False,
"custom_boot_script": '',
}
},
windows=['MainWindow.py'],
)
似乎可以工作,因为我在“dist”文件夹中获取可执行应用程序,当我运行它时,它加载正常,当我插入相同的数据时,它会落在特殊字符(变音符号)上。 100个条目我成功插入60,第一次出现重音字符导致可执行文件失败并显示错误消息
(,UnicodeEncodeError('ascii', u'Citro \ xebnCX',5,6,'序数不在范围内(128)'),)
恰巧我正在将CitroenCX插入表中,'e'是法语重音字符
我不知道为什么当我从我的eclipse环境运行python / pyqt应用程序时,我没有错误,但是一旦我创建了一个可执行文件,我就会收到这个错误。
我应该在设置文件中进行哪些更改
<小时/> 谢谢。我应用了解决方案
def setencoding():
encoding = "ascii"
if 0:
import locale
loc = locale.getdefaultlocale()
if loc[1]:
encoding = loc[1]
if 0: #changes comes here, change 0 to 1
encoding = "undefined" #the encoding you want
if encoding != "ascii":
sys.setdefaultencoding(encoding)
它对我有用。不是原始解决方案,只能在SO How to set default encoding in Python (setdefaultencoding() function does not exist)?
上看到它答案 0 :(得分:1)
不同之处在于不同环境选择的默认编码。 Unicode到stdin或stdout的任何I / O都将使用默认编码触发编码或解码。如果该编码为'ascii'
,那么任何重音字符都将失败。
您可以检查默认值:
import sys
sys.getdefaultencoding()
sys.stdin.encoding
sys.stdout.encoding