[PyQt] UnicodeEncodeError

MICHAEL FERRARO michael at possibleworlds.com
Wed Jul 20 22:30:43 BST 2016


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 at 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 at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160720/e797bb7c/attachment-0001.html>


More information about the PyQt mailing list