Spyder - Python - UnicodeDecodeError: 'ascii' codec can't decode - python

TL, DR: A python file encounters the UnicodeDecodeError when ran in Spyder, but works when I ran it in command line.
I downloaded a Python module that wraps a C implementation of a suffix tree. After built, it was ran in Spyder but I got the following error:
runfile('F:/src/suffix_tree-2.1/build/lib.win32-2.7/suffix_tree.py',
wdir='F:/src/suffix_tree-2.1/build/lib.win32-2.7')
Traceback (most recent call last):
File
"G:\IDE\python\Anaconda\lib\site-packages\IPython\core\interactiveshell.py",
line 3052, in run_code
self.showtraceback()
File
"G:\IDE\python\Anaconda\lib\site-packages\IPython\core\interactiveshell.py",
line 1851, in showtraceback
value, tb, tb_offset=tb_offset)
File
"G:\IDE\python\Anaconda\lib\site-packages\IPython\core\ultratb.py",
line 1240, in structured_traceback
self, etype, value, tb, tb_offset, number_of_lines_of_context)
File
"G:\IDE\python\Anaconda\lib\site-packages\IPython\core\ultratb.py",
line 1157, in structured_traceback
self, etype, value, elist, tb_offset, number_of_lines_of_context
File
"G:\IDE\python\Anaconda\lib\site-packages\IPython\core\ultratb.py",
line 511, in structured_traceback
lines = ''.join(self._format_exception_only(etype, value))
File
"G:\IDE\python\Anaconda\lib\site-packages\IPython\core\ultratb.py",
line 623, in _format_exception_only
Colors.Normal, s))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb2 in position
20: ordinal not in range(128)
This error appears right after I import _suffix_tree.pyd, even before any other operations.
However, if I run the file in command line (cmd), it works successfully without any error.
PS: I use Windows, and my user name are ASCII characters.

You are possibly trying to open a file encoded in a different format of your system, you should check it and set Spyder using the commands below.
import sys
reload(sys)
sys.setdefaultencoding('utf8')
If this does not work, check the version of the Python kernel of the program. You can get this message trying to run an Python 3 code in an Spyder 2.

Related

pywinauto save (permant) control identifiers to variable

I am trying to save control identifiers to a variable.
The reason is that: i use main_dlg.print_control_identifiers(filename="control_ids.text") but i am getting this error:
Traceback (most recent call last):
File "run_tests.py", line 7, in <module>
main_dlg.print_control_identifiers(filename="control_ids.text")
File "C:\python\lib\site-packages\pywinauto\application.py", line 696, in prin
t_control_identifiers
print_identifiers([this_ctrl, ], log_func=log_func)
File "C:\python\lib\site-packages\pywinauto\application.py", line 685, in prin
t_identifiers
print_identifiers(ctrl.children(), current_depth + 1, log_func)
File "C:\python\lib\site-packages\pywinauto\application.py", line 681, in prin
t_identifiers
log_func(output)
File "C:\python\lib\site-packages\pywinauto\application.py", line 694, in log_
func
log_file.write(str(msg) + os.linesep)
File "C:\python\lib\codecs.py", line 721, in write
return self.writer.write(data)
File "C:\python\lib\codecs.py", line 377, in write
data, consumed = self.encode(object, self.errors)
File "C:\python\lib\encodings\cp1252.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 71-76: c
haracter maps to <undefined>
because the program i am trying to control has greek characters inside.
So, i decided to save the control identifiers to a variable and then save it with the right encoding to a txt file.
A simple solution i thought is:
from pywinauto.application import Application
import os
app = Application(backend="uia").start("C:/python/Lib/site-packages/QtDesigner/designer.exe")
main_dlg = app.QtDesigner
main_dlg.wait('visible')
main_dlg.print_control_identifiers()
and then:
python script_name.py > output.txt
after that in output.txt file there are the control identifiers of the program (in this example QtDesigner).

How to use psutil.Popen with unicode commands on Python 2

Hey I'm trying to execute the following command (using psutil.Popen with python 2.7):
"C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" "C:\docs\ת.xlsm"
Using this code:
dir = u"C:\\docs"
doc = os.listdir(dir)[0]
full_path = os.path.join(dir, doc)
command = u"\"C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\EXCEL.EXE\" \"{}\"".format(full_path)
process = psutil.Popen(command)
But I'm getting this exception:
process = psutil.Popen(command)
File "C:\Python27\lib\site-packages\psutil\__init__.py", line 1370, in __init__
self.__subproc = subprocess.Popen(*args, **kwargs)
File "C:\Python27\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u05ea' in position 102: ordinal not in range(128)
I have found this related question:
subprocess.Popen with a unicode path. But every given answer there doesn't work for me.
Using subprocess.Popen(command.encode(locale.getpreferredencoding())), throws the following exception:
Traceback (most recent call last):
File "scanner_watcher.py", line 53, in _execute
self.scanner.execute(path)
File "scanner_watcher.py", line 356, in execute
self._execute(file_path)
File "scanner_watcher.py", line 201, in _execute
self.open_scanner(file_path, file_package)
File "scanner_watcher.py", line 287, in open_scanner
self.scanner_process = psutil.Popen(command.encode(locale.getpreferredencoding()))
File "C:\Python27\lib\encodings\cp1252.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u05ea' in position 102: character maps to <undefined>
Using path.encode('mbcs') turns all the Unicode characters into question marks.
I don't want to use os.startfile because I need to use different commands on the program and then handle the opened process (when the os.startfile doesn't allow that).
I've found this modified Popen:
https://gist.github.com/vaab/2ad7051fc193167f15f85ef573e54eb9 But this code was not thoroughly tested.
Is there a correct way to use Popen with a Unicode command in python 2.7?
Thanks.
The issue is your use of double-quotes when defining command. The way it's written splits the statement into multiple strings in all the wrong places. Substituting single-quotes for the first and final quotes (open and close of string) fixed the issue for me.
command = u'\"C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\EXCEL.EXE\" \"{}\"'.format(full_path)

rabbitmqadmin can't handle unicode?

I might have stumbled into something that appears to be a bug with how rabbitmqadmin handles (or in fact, doesn't handle) unicode.
If I have a queue that has unicode characters in queue name the rabbitmqadmin command fails with UnicodeEncodeErrors.
I'm running RabbitMQ 3.6.6 on Mac, installed via Homebrew. Both the admin command and server are of same version.
Steps how to reproduce the queue name issue:
Create a queue with unicode in it: 'rabbitmqadmin declare queue name=ööö'.
List queues: 'rabbitmqadmin list queues'
Output:
Traceback (most recent call last):
File "/usr/local/sbin/rabbitmqadmin", line 1007, in <module>
main()
File "/usr/local/sbin/rabbitmqadmin", line 413, in main
method()
File "/usr/local/sbin/rabbitmqadmin", line 588, in invoke_list
format_list(self.get(uri), cols, obj_info, self.options)
File "/usr/local/sbin/rabbitmqadmin", line 705, in format_list
formatter_instance.display(json_list)
File "/usr/local/sbin/rabbitmqadmin", line 716, in display
(columns, table) = self.list_to_table(json.loads(json_list), depth)
File "/usr/local/sbin/rabbitmqadmin", line 770, in list_to_table
add('', 1, item, add_to_row)
File "/usr/local/sbin/rabbitmqadmin", line 749, in add
fun(column, subitem)
File "/usr/local/sbin/rabbitmqadmin", line 756, in add_to_row
row[column_ix[col]] = str(val)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 12-13: ordinal not in range(128)
I have similar issues with 'rabbitmqadmin get queue=' when the message payload contains unicode character(s).
I think you are right, you can use python3 , check this answer it explains why with python3 works.
EDIT
1 - I filed an issue
2 - Fixed to the version: 3.6.7 see the PR

Python rdflib Not Parsing Creative Commons License Information Correctly

I was using rdflib version 3.2.3 and everything was working fine. After upgrading to 4.0.1 I started getting the error:
RDFa parsing Error! 'ascii' codec can't decode byte 0xc3 in position 5454: ordinal not in range(128)
I tried various way to make this work but so far have not succeeded. Below are my attempts.
In each case I:
from rdflib import Graph
First attempt:
>>> lg =Graph()
>>> len(lg.parse('http://creativecommons.org/licenses/by/3.0/'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/alex/Projects/RDF/rdfEnv/local/lib/python2.7/site-packages/rdflib/graph.py", line 1002, in parse
parser.parse(source, self, **args)
File "/home/alex/Projects/RDF/rdfEnv/local/lib/python2.7/site-packages/rdflib/plugins/parsers/structureddata.py", line 268, in parse
vocab_cache=vocab_cache)
File "/home/alex/Projects/RDF/rdfEnv/local/lib/python2.7/site-packages/rdflib/plugins/parsers/structureddata.py", line 148, in _process
_check_error(processor_graph)
File "/home/alex/Projects/RDF/rdfEnv/local/lib/python2.7/site-packages/rdflib/plugins/parsers/structureddata.py", line 57, in _check_error
raise Exception("RDFa parsing Error! %s" % msg)
Exception: RDFa parsing Error! 'ascii' codec can't decode byte 0xc3 in position 4801: ordinal not in range(128)
Second attempt:
>>> lg =Graph()
>>> len(lg.parse('http://creativecommons.org/licenses/by/3.0/rdf'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/alex/Projects/RDF/rdfEnv/local/lib/python2.7/site-packages/rdflib/graph.py", line 1002, in parse
parser.parse(source, self, **args)
File "/home/alex/Projects/RDF/rdfEnv/local/lib/python2.7/site-packages/rdflib/plugins/parsers/rdfxml.py", line 570, in parse
self._parser.parse(source)
File "/usr/lib/python2.7/xml/sax/expatreader.py", line 107, in parse
xmlreader.IncrementalParser.parse(self, source)
File "/usr/lib/python2.7/xml/sax/xmlreader.py", line 123, in parse
self.feed(buffer)
File "/usr/lib/python2.7/xml/sax/expatreader.py", line 207, in feed
self._parser.Parse(data, isFinal)
File "/usr/lib/python2.7/xml/sax/expatreader.py", line 349, in end_element_ns
self._cont_handler.endElementNS(pair, None)
File "/home/alex/Projects/RDF/rdfEnv/local/lib/python2.7/site-packages/rdflib/plugins/parsers/rdfxml.py", line 160, in endElementNS
self.current.end(name, qname)
File "/home/alex/Projects/RDF/rdfEnv/local/lib/python2.7/site-packages/rdflib/plugins/parsers/rdfxml.py", line 461, in property_element_end
current.data, literalLang, current.datatype)
File "/home/alex/Projects/RDF/rdfEnv/local/lib/python2.7/site-packages/rdflib/term.py", line 541, in __new__
raise Exception("'%s' is not a valid language tag!"%lang)
Exception: 'i18n' is not a valid language tag!
Third attempt: gives no errors but also does not give any results
>>> lg =Graph()
>>> len(lg.parse('http://creativecommons.org/licenses/by/3.0/rdf', format='rdfa'))
0
So someone please tell me what I am dong wrong! :)
As Graham replied on the rdflib mailinglist, there is a html5lib problem - we will pin it correctly for python 2 for the next release, but for now just do:
pip install html5lib==0.95
The second problem is a problem in the data from creative commons, "i18n" really isn't a valid language tag according to rfc5646. I added the check, but in retrospect it seems to strict to raise an exception. I guess I'll change it to a warning.

What caused this traceback? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
UnicodeDecodeError when passing GET data in Python/AppEngine
I get the following traceback locally and on production when trying to submit a form. Can you explain where I should look or should I start making debugging statements to see where in the code the exception occurs?
--> --> -->
Traceback (most recent call last):
File "/media/Lexar/montao/google/appengine/tools/dev_appserver.py", line 3858, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "/media/Lexar/montao/google/appengine/tools/dev_appserver.py", line 3792, in _Dispatch
base_env_dict=env_dict)
File "/media/Lexar/montao/google/appengine/tools/dev_appserver.py", line 580, in Dispatch
base_env_dict=base_env_dict)
File "/media/Lexar/montao/google/appengine/tools/dev_appserver.py", line 2918, in Dispatch
self._module_dict)
File "/media/Lexar/montao/google/appengine/tools/dev_appserver.py", line 2822, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "/media/Lexar/montao/google/appengine/tools/dev_appserver.py", line 2704, in ExecuteOrImportScript
script_module.main()
File "/media/Lexar/montao/classifiedsmarket/main.py", line 2497, in main
util.run_wsgi_app(application)
File "/media/Lexar/montao/google/appengine/ext/webapp/util.py", line 98, in run_wsgi_app
run_bare_wsgi_app(add_wsgi_middleware(application))
File "/media/Lexar/montao/google/appengine/ext/webapp/util.py", line 116, in run_bare_wsgi_app
result = application(env, _start_response)
File "/media/Lexar/montao/google/appengine/ext/webapp/__init__.py", line 655, in __call__
response.wsgi_write(start_response)
File "/media/Lexar/montao/google/appengine/ext/webapp/__init__.py", line 274, in wsgi_write
body = self.out.getvalue()
File "/usr/lib/python2.6/StringIO.py", line 270, in getvalue
self.buf += ''.join(self.buflist)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)
The error manifested itself in /usr/lib/python2.6/StringIO.py i.e. the Python StringIO module. We don't need to read too far into that source file (line 49) to find this warning:
The StringIO object can accept either Unicode or 8-bit strings, but
mixing the two may take some care. If both are used, 8-bit strings that
cannot be interpreted as 7-bit ASCII (that use the 8th bit) will
cause a UnicodeError to be raised when getvalue() is called.
Bingo! And the warning is repeated again in the getvalue() method. Note that the warning is ancient; it mentions UnicodeError instead of UnicodeDecodeError, but you get the drift.
I'd suggest patching the module so that it displays what's in the bag when the error happens. Wrap up the offending statement at line 270 like this:
if self.buflist:
try:
self.buf += ''.join(self.buflist)
except UnicodeDecodeError:
import sys
print >> sys.stderr, "*** error context: buf=%r buflist=%r" % (self.buf, self.buflist)
raise
self.buflist = []
return self.buf
If the idea of patching a Python-supplied module in situ horrifies you, put the patched version in a directory that's earlier in sys.path than /usr/lib/python2.6.
Here's an example of mixing non-ASCII str and unicode:
>>> from StringIO import StringIO
>>> f = StringIO()
>>> f.write('ascii')
>>> f.write(u'\u1234'.encode('utf8'))
>>> f.write(u'\u5678')
>>> f.getvalue()
*** error context: buf='' buflist=['ascii', '\xe1\x88\xb4', u'\u5678']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\python26\lib\StringIO.py", line 271, in getvalue
self.buf += ''.join(self.buflist)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe1 in position 0: ordinal not in range(128)
>>>
Then you can run your application and look at what is in buflist: which parts are data that you wrote, and which are provided by GAE. You need to look at the GAE docs to see whether it is expecting str contents (with what encoding?) or unicode contents, and adjust your code accordingly.

Categories

Resources