[Py2exe-users] unicode error after py2exe

Hi.
I'm running python 2.6, wx 2.8, and py2exe .6.9 on XP. I get a unicode
error in a wxpython program after py2exe, in the .exe version. The
error doesn't occur in the .py python program. It didn't look like the
encoding issues on this website were the same as my issue.
The error code is: 'UnicodeEncodeError: 'ascii' codec can't encode
character u'\u2013' in position 20: ordinal not in range(128)'
Here's the area of code where it's messing up. There's a nested
try...except loop as I was trying different things to get it to work.
try:
entry_dict[cols[index]] = str(col) #column=value
except:
checkfile.close()
try:
newstr = ''
bad_pos = 0
for char in col.encode():
bad_pos += 1
if ord(char) < 128:
newstr = char
entry_dict[cols[index]] = str(col)
except:
entry_dict[cols[index]] = str(col[0:bad_pos] +
col[bad_pos+1:])

Thread view

Hi.
I'm running python 2.6, wx 2.8, and py2exe .6.9 on XP. I get a unicode
error in a wxpython program after py2exe, in the .exe version. The
error doesn't occur in the .py python program. It didn't look like the
encoding issues on this website were the same as my issue.
The error code is: 'UnicodeEncodeError: 'ascii' codec can't encode
character u'\u2013' in position 20: ordinal not in range(128)'
Here's the area of code where it's messing up. There's a nested
try...except loop as I was trying different things to get it to work.
try:
entry_dict[cols[index]] = str(col) #column=value
except:
checkfile.close()
try:
newstr = ''
bad_pos = 0
for char in col.encode():
bad_pos += 1
if ord(char) < 128:
newstr = char
entry_dict[cols[index]] = str(col)
except:
entry_dict[cols[index]] = str(col[0:bad_pos] +
col[bad_pos+1:])

Allen,
Roach, Allen (IHS/NPA) wrote:
> This is the output:
> b4 try
> u'151 - 835 File Format\u2013Technical Review' >as is<
I believe this is a "-" encoded in utf-8 - at least that what is shown
in my shell.
What encoding are you using when you run it as a .py?
i.e. can you add this to your code:
import sys
print sys.getdefaultencoding()
>
> I did try cutting and concatenating the string without including the
> unicode character. That didn't work on the .exe program. Though, had
> no problems on the python version.
I am guessing that you are running utf-8, maybe set in sitecustomize.py,
but when running in .exe it is using ascii. If this is your problem then
the code I posted earlier in this thread should fix your problem.
Werner

Hi,
Roach, Allen (IHS/NPA) wrote:
> Hi.
>
> I’m running python 2.6, wx 2.8, and py2exe ..6.9 on XP. I get a unicode
> error in a wxpython program after py2exe, in the .exe version. The
> error doesn’t occur in the .py python program. It didn’t look like the
> encoding issues on this website were the same as my issue.
Are you using "ascii" encoding when you run it as .py?
The following is not really a correction to your problem, but in my case
it reduce the number of 'decode/encode' calls in the application.
In my app (using wxPython 2.8.x Unicode) I added the following to my
app.py file.
# people say one should leave this alone and use decode/encode, or
define this
# in sitecustomize.py
# either of them don't really work for me, so as long as the following does
# this is what I will do
if hasattr(sys, "frozen"): #Py2Exe does not run Site.py
sys.setdefaultencoding('utf-8')
del sys.setdefaultencoding
else: #The Python interpreter needs to reload the
function
reload(sys)
sys.setdefaultencoding('utf-8')
del sys.setdefaultencoding
"people" in the above are different posts on Python lists.
All my .py files are in utf-8 encoding and have the following in the
first line "# -*- coding: utf-8 -*-#".
Then I make sure that any external data being read in is either utf-8 or
I do the encode/decode call's.
Werner