[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyQt] UnicodeEncodeError
From:       MICHAEL FERRARO <michael () possibleworlds ! com>
Date:       2016-07-20 21:30:43
Message-ID: 8AE7D74D-4613-41A7-97DF-7C7B92108D85 () possibleworlds ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Solved!

Although it was probably obvious to most of the community.

I discovered that the problem was I needed to encode the
string before I passed it to psd.open().  

i.e
	file = psd.open (fname.encode (‘utf-8'))

the QFileDialog.getExistingDirectory() return unicode and it needs
to be converted (encoded) into a byte sequence (python string)
to pass to the extension.

Thanks for listening

m



> On Jul 20, 2016, at 2:37 PM, MICHAEL FERRARO <michael@possibleworlds.com> wrote:
> 
> Hello
> 
> I am running python 2.7.11 + PytQt 5.6 on MaxOS 10.11.5
> I have installed an extension that I built called ‘psd' that has an open() method \
> that takes a path to a PSD file 
> when I run the script the line containing 'type (path)' command returns:
> 	<type 'unicode'>
> 
> but the next line containing ‘file = psd.open (fname)' fails with:
> 	UnicodeEncodeError: 'ascii' codec can't encode character u'\u2022' 
> 
> It seems as if there is some implicit type conversion being done when the filename \
> is passed to the psd.open() method. If I try the command directly at the python \
> command line it works fine and opens the file so I don't think it is the extension \
> causing the problem. 
> 
> any idea what I am doing wrong??
> 
> I have this at the top of the file:
> 
> #!/usr/bin/python
> # -*- coding: utf-8 -*-
> 
> 
> and this is the code fragment:
> 
> def LoadImageFile (self, fname, defer=False):		
> 	(path,file) = os.path.split (fname)
> 	(root,extn) = os.path.splitext (file)
> 		
> 	if extn != '.psd':
> 		return
> 		
> 	print "in LoadImageFIle fname: " + fname
> 	print type (fname)
> 
> 	file = psd.open (fname)
> 	iw = float (file.imagecols)
> 	ih = float (file.imagerows)
> 
> 
> 
> and this is the traceback 
> 
> in LoadImageFIle fname: \
> /Volumes/diablo-external/Dropbox/•Production/gail/--EXPORTS--/new-December/Drawings/gail-armsBack-Rebuild-01.VP/mon_head-01-0.psd
>  <type 'unicode'>
> Traceback (most recent call last):
> File "gfx-Teatro.py", line 267, in <module>
> main()
> File "gfx-Teatro.py", line 37, in main
> app.Run(args)
> File "gfx-Teatro.py", line 81, in Run
> self.InitializeProject ()
> File "gfx-Teatro.py", line 94, in InitializeProject
> self.NewDefaultScene (self.controller.job, "Performance")
> File "gfx-Teatro.py", line 110, in NewDefaultScene
> scene.LoadXsheets ([dir])				
> File "/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Scene.py", \
> line 511, in LoadXsheets xsheet = self.LoadXsheets (flist, into, item)
> File "/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Scene.py", \
> line 511, in LoadXsheets xsheet = self.LoadXsheets (flist, into, item)
> File "/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Scene.py", \
> line 511, in LoadXsheets xsheet = self.LoadXsheets (flist, into, item)
> File "/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Scene.py", \
> line 511, in LoadXsheets xsheet = self.LoadXsheets (flist, into, item)
> File "/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Scene.py", \
> line 521, in LoadXsheets background.LoadXsheets ([file], dir=path, level=0, \
> item=self.tranItem) File \
> "/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Background.py", \
> line 224, in LoadXsheets entry.LoadXsheets (fname)
> File "/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Xsheet.py", \
> line 341, in LoadXsheets self.Read(project)
> File "/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Xsheet.py", \
> line 504, in Read entry.Read (xfile)
> File "/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Xcolumn.py", \
> line 312, in Read entry.Load ()
> File "/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Xreference.py", \
> line 138, in Load plate.LoadImageFile (fname)
> File "/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Layout.py", \
> line 1218, in LoadImageFile file = psd.open (fname)
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u2022' in position 33: \
> ordinal not in range(128) e-c908-38136:Teatro michael_ferraro$ python -V
> 
> _______________________________________________
> PyQt mailing list    PyQt@riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt


[Attachment #5 (unknown)]

<html><head><meta http-equiv="Content-Type" content="text/html \
charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; \
-webkit-line-break: after-white-space;" class="">Solved!<div class=""><br \
class=""></div><div class="">Although it was probably obvious to most of the \
community.</div><div class=""><br class=""><div class="">I discovered that the \
problem was I needed to encode the</div><div class="">string before I passed it to \
psd.open(). &nbsp;</div><div class=""><br class=""></div><div class="">i.e</div><div \
class=""><span class="Apple-tab-span" style="white-space:pre">	</span>file \
=&nbsp;psd.open (fname.encode (‘utf-8'))</div><div class=""><br class=""></div><div \
class="">the QFileDialog.getExistingDirectory() return unicode and it needs</div><div \
class="">to be converted (encoded) into a byte sequence (python string)</div><div \
class="">to pass to the extension.</div><div class=""><br class=""></div><div \
class="">Thanks for listening</div><div class=""><br class=""></div><div \
class="">m</div><div class=""><br class=""></div><div class=""><br \
class=""></div><div class=""><br class=""></div><div class=""><div class=""><div \
class=""><div><blockquote type="cite" class=""><div class="">On Jul 20, 2016, at 2:37 \
PM, MICHAEL FERRARO &lt;<a href="mailto:michael@possibleworlds.com" \
class="">michael@possibleworlds.com</a>&gt; wrote:</div><br \
class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" \
content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; \
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hello<div \
class=""><br class=""></div><div class="">I am running python 2.7.11 + PytQt 5.6 on \
MaxOS 10.11.5</div><div class="">I have installed an extension that I built called \
‘psd' that has an open() method that takes a path to a PSD file</div><div \
class=""><br class=""></div><div class="">when I run the script the line containing \
'type (path)' command returns:</div><div class=""><font face="Menlo" class=""><span \
style="font-size: 11px;" class=""><span class="Apple-tab-span" \
style="white-space:pre">	</span>&lt;type 'unicode'&gt;</span></font></div><div \
class=""><font face="Menlo" class=""><span style="font-size: 11px;" class=""><br \
class=""></span></font></div><div class="">but the next line containing ‘file = \
psd.open (fname)' fails with:</div><div class=""><div style="margin: 0px; font-size: \
11px; line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class=""><span \
class="Apple-tab-span" style="white-space:pre">	</span>UnicodeEncodeError: 'ascii' \
codec can't encode character u'\u2022'&nbsp;</span></div></div><div style="margin: \
0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br \
class=""></div><div style="margin: 0px; line-height: normal;" class="">It seems as if \
there is some implicit type conversion being done when the filename is passed to the \
psd.open() method.</div><div style="margin: 0px; line-height: normal;" class="">If I \
try the command directly at the python command line it works fine and opens the file \
so I don't think it is the extension causing the problem.</div><div style="margin: \
0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; \
line-height: normal;" class=""><br class=""></div><div style="margin: 0px; \
line-height: normal;" class="">any idea what I am doing wrong??</div><div \
style="margin: 0px; line-height: normal;" class=""><br class=""></div><div \
style="margin: 0px; line-height: normal;" class="">I have this at the top of the \
file:</div><div style="margin: 0px; line-height: normal;" class=""><br \
class=""></div><div style="margin: 0px; line-height: normal;" \
class="">#!/usr/bin/python</div><div style="margin: 0px; line-height: normal;" \
class=""># -*- coding: utf-8 -*-</div><div style="margin: 0px; line-height: normal;" \
class=""><br class=""></div><div style="margin: 0px; line-height: normal;" \
class=""><br class=""></div><div style="margin: 0px; line-height: normal;" \
class="">and this is the code fragment:</div><div style="margin: 0px; line-height: \
normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" \
class=""><div style="margin: 0px; line-height: normal;" class=""><font face="Menlo" \
class=""><span style="font-size: 11px;" class="">def LoadImageFile (self, fname, \
defer=False):<span class="Apple-tab-span" style="white-space: \
pre;">		</span></span></font></div><div style="margin: 0px; line-height: normal;" \
class=""><font face="Menlo" class=""><span style="font-size: 11px;" class=""><span \
class="Apple-tab-span" style="white-space:pre">	</span>(path,file) = os.path.split \
(fname)</span></font></div><div style="margin: 0px; line-height: normal;" \
class=""><font face="Menlo" class=""><span style="font-size: 11px;" class=""><span \
class="Apple-tab-span" style="white-space:pre">	</span>(root,extn) = os.path.splitext \
(file)</span></font></div><div style="margin: 0px; line-height: normal;" \
class=""><span class="Apple-tab-span" style="white-space: pre; font-size: \
11px;"><font face="Menlo" class="">		</font></span></div><div style="margin: 0px; \
line-height: normal;" class=""><font face="Menlo" class=""><span \
class="Apple-tab-span" style="font-size: 11px; white-space: pre;">	</span><span \
style="font-size: 11px;" class="">if extn != '.psd':</span></font></div><div \
style="margin: 0px; line-height: normal;" class=""><span class="Apple-tab-span" \
style="font-size: 11px; font-family: Menlo; white-space: pre;">		</span><span \
style="font-size: 11px; font-family: Menlo;" class="">return</span></div><div \
style="margin: 0px; line-height: normal;" class=""><span class="Apple-tab-span" \
style="white-space: pre; font-size: 11px;"><font face="Menlo" \
class="">		</font></span></div><div style="margin: 0px; line-height: normal;" \
class=""><span class="Apple-tab-span" style="font-size: 11px; font-family: Menlo; \
white-space: pre;">	</span><span style="font-size: 11px; font-family: Menlo;" \
class="">print "in LoadImageFIle fname: " + fname</span></div><div style="margin: \
0px; line-height: normal;" class=""><font face="Menlo" class=""><span \
class="Apple-tab-span" style="font-size: 11px; white-space: pre;">	</span><span \
style="font-size: 11px;" class="">print type (fname)</span></font></div><div \
style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class=""><span \
style="font-size: 11px;" class=""><br class=""></span></font></div><div \
style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class=""><span \
style="font-size: 11px;" class=""><span class="Apple-tab-span" \
style="white-space:pre">	</span>file = psd.open (fname)</span></font></div><div \
style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class=""><span \
style="font-size: 11px;" class=""><span class="Apple-tab-span" \
style="white-space:pre">	</span>iw = float (file.imagecols)</span></font></div><div \
style="margin: 0px; line-height: normal;" class=""><font face="Menlo" class=""><span \
style="font-size: 11px;" class=""><span class="Apple-tab-span" \
style="white-space:pre">	</span>ih = float (file.imagerows)</span></font></div><div \
style="margin: 0px; line-height: normal;" class=""><br class=""></div><div \
style="margin: 0px; line-height: normal;" class=""><br class=""></div><div \
style="margin: 0px; line-height: normal;" class=""><br class=""></div><div \
style="margin: 0px; line-height: normal;" class="">and this is the \
traceback&nbsp;</div><div style="margin: 0px; line-height: normal;" class=""><br \
class=""></div><div style="margin: 0px; line-height: normal;" class=""><div \
style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" \
class=""><span style="font-variant-ligatures: no-common-ligatures" class="">in \
LoadImageFIle fname: \
/Volumes/diablo-external/Dropbox/•Production/gail/--EXPORTS--/new-December/Drawings/gail-armsBack-Rebuild-01.VP/mon_head-01-0.psd</span></div><div \
style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" \
class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&lt;type \
'unicode'&gt;</span></div><div style="margin: 0px; font-size: 11px; line-height: \
normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: \
no-common-ligatures" class="">Traceback (most recent call last):</span></div><div \
style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" \
class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; \
File "gfx-Teatro.py", line 267, in &lt;module&gt;</span></div><div style="margin: \
0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; \
main()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; \
font-family: Menlo;" class=""><span style="font-variant-ligatures: \
no-common-ligatures" class="">&nbsp; File "gfx-Teatro.py", line 37, in \
main</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; \
font-family: Menlo;" class=""><span style="font-variant-ligatures: \
no-common-ligatures" class="">&nbsp; &nbsp; app.Run(args)</span></div><div \
style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" \
class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; \
File "gfx-Teatro.py", line 81, in Run</span></div><div style="margin: 0px; font-size: \
11px; line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; \
self.InitializeProject ()</span></div><div style="margin: 0px; font-size: 11px; \
line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; File \
"gfx-Teatro.py", line 94, in InitializeProject</span></div><div style="margin: 0px; \
font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; \
self.NewDefaultScene (self.controller.job, "Performance")</span></div><div \
style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" \
class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; \
File "gfx-Teatro.py", line 110, in NewDefaultScene</span></div><div style="margin: \
0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; \
scene.LoadXsheets ([dir])<span class="Apple-tab-span" \
style="white-space:pre">				</span></span></div><div style="margin: 0px; font-size: \
11px; line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; File \
"/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Scene.py", line \
511, in LoadXsheets</span></div><div style="margin: 0px; font-size: 11px; \
line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; xsheet = \
self.LoadXsheets (flist, into, item)</span></div><div style="margin: 0px; font-size: \
11px; line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; File \
"/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Scene.py", line \
511, in LoadXsheets</span></div><div style="margin: 0px; font-size: 11px; \
line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; xsheet = \
self.LoadXsheets (flist, into, item)</span></div><div style="margin: 0px; font-size: \
11px; line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; File \
"/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Scene.py", line \
511, in LoadXsheets</span></div><div style="margin: 0px; font-size: 11px; \
line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; xsheet = \
self.LoadXsheets (flist, into, item)</span></div><div style="margin: 0px; font-size: \
11px; line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; File \
"/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Scene.py", line \
511, in LoadXsheets</span></div><div style="margin: 0px; font-size: 11px; \
line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; xsheet = \
self.LoadXsheets (flist, into, item)</span></div><div style="margin: 0px; font-size: \
11px; line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; File \
"/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Scene.py", line \
521, in LoadXsheets</span></div><div style="margin: 0px; font-size: 11px; \
line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; \
background.LoadXsheets ([file], dir=path, level=0, \
item=self.tranItem)</span></div><div style="margin: 0px; font-size: 11px; \
line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; File \
"/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Background.py", \
line 224, in LoadXsheets</span></div><div style="margin: 0px; font-size: 11px; \
line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; \
entry.LoadXsheets (fname)</span></div><div style="margin: 0px; font-size: 11px; \
line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; File \
"/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Xsheet.py", line \
341, in LoadXsheets</span></div><div style="margin: 0px; font-size: 11px; \
line-height: normal; font-family: Menlo;" class=""><span \
style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; \
self.Read(project)</span></div><div style="margin: 0px; font-size: 11px; line-height: \
normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: \
no-common-ligatures" class="">&nbsp; File \
"/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Xsheet.py", line \
504, in Read</span></div><div style="margin: 0px; font-size: 11px; line-height: \
normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: \
no-common-ligatures" class="">&nbsp; &nbsp; entry.Read (xfile)</span></div><div \
style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" \
class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; \
File "/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Xcolumn.py", \
line 312, in Read</span></div><div style="margin: 0px; font-size: 11px; line-height: \
normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: \
no-common-ligatures" class="">&nbsp; &nbsp; entry.Load ()</span></div><div \
style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" \
class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; \
File "/Volumes/diablo-external/Dropbox/Development/-Applications-/Teatro/Xreference.py", \


[Attachment #6 (text/plain)]

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic