I'm trying to get some data from one web page and write these data into xlsx file. Everything seems good but the Encoding error probably raises if it tries to write it into xlsx file during CLOSING the file.
ERROR:
Traceback (most recent call last):
File "C:/Users/Milano/PycharmProjects/distrelec/crawler.py", line 429, in <module>
temp_file_to_xlsx()
File "C:/Users/Milano/PycharmProjects/distrelec/crawler.py", line 119, in temp_file_to_xlsx
wb.close()
File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 295, in close
self._store_workbook()
File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 518, in _store_workbook
xml_files = packager._create_package()
File "C:\Python27\lib\site-packages\xlsxwriter\packager.py", line 134, in _create_package
self._write_workbook_file()
File "C:\Python27\lib\site-packages\xlsxwriter\packager.py", line 174, in _write_workbook_file
workbook._assemble_xml_file()
File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 464, in _assemble_xml_file
self._write_sheets()
File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 1455, in _write_sheets
self._write_sheet(worksheet.name, id_num, worksheet.hidden)
File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 1472, in _write_sheet
self._xml_empty_tag('sheet', attributes)
File "C:\Python27\lib\site-packages\xlsxwriter\xmlwriter.py", line 80, in _xml_empty_tag
self.fh.write("<%s/>" % tag)
File "C:\Python27\lib\codecs.py", line 694, in write
return self.writer.write(data)
File "C:\Python27\lib\codecs.py", line 357, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 23: ordinal not in range(128)
To find out where is the problem I've edited codecs module:
def write(self, object):
""" Writes the object's contents encoded to self.stream.
"""
try:
data, consumed = self.encode(object, self.errors)
self.stream.write(data)
except:
print object
print repr(object)
raise Exception
The output is:
<sheet name="Android PC–APC" sheetId="42" r:id="rId42"/>
'<sheet name="Android PC\xe2\x80\x93APC" sheetId="42" r:id="rId42"/>'
temp_file_to_xlsx()
File "C:/Users/Milano/PycharmProjects/distrelec/crawler.py", line 119, in temp_file_to_xlsx
wb.close()
File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 295, in close
self._store_workbook()
File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 518, in _store_workbook
xml_files = packager._create_package()
File "C:\Python27\lib\site-packages\xlsxwriter\packager.py", line 134, in _create_package
self._write_workbook_file()
File "C:\Python27\lib\site-packages\xlsxwriter\packager.py", line 174, in _write_workbook_file
workbook._assemble_xml_file()
File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 464, in _assemble_xml_file
self._write_sheets()
File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 1455, in _write_sheets
self._write_sheet(worksheet.name, id_num, worksheet.hidden)
File "C:\Python27\lib\site-packages\xlsxwriter\workbook.py", line 1472, in _write_sheet
self._xml_empty_tag('sheet', attributes)
File "C:\Python27\lib\site-packages\xlsxwriter\xmlwriter.py", line 80, in _xml_empty_tag
self.fh.write("<%s/>" % tag)
File "C:\Python27\lib\codecs.py", line 699, in write
return self.writer.write(data)
File "C:\Python27\lib\codecs.py", line 363, in write
raise Exception
Exception
What should I do with that please?
You have to decode your input data with the correct encoding, which seems to be 'utf-8'.
You may want to look at this:
Example: Simple Unicode with Python 2
Related
So I have created a Python program which will delete all the public links to files in our company Dropbox. I will occasionally log something that involves a filename, for instance if it finds a publicly shared file while in the file checking process:
filepath = file.path_lower
...
dmsg = filepath + " public"
logger.debug(dmsg)
This works fine for most filepaths. However, we have some filepaths with Chinese characters in them, and these are causing issues. My logging setup looks like this:
import logging
...
fmt = "%(asctime)s %(levelname)s(%(name)s): %(message)s"
# Dropbox logs a lot of things; set up where they will go
logging.basicConfig(
filename="dropbox default logging.log",
encoding='utf-8',
format=fmt,
level=logging.DEBUG)
# we want a separate file for the messages we actually care about
handler = logging.FileHandler("unsharing program.log", encoding="utf-8")
formatter = logging.Formatter(fmt)
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
logger = logging.getLogger("UnsharingProgram")
# Before I added this handler remove code I had some.. issues. Don't touch
for handler in logger.handlers[:]:
logger.removeHandler(handler)
logger.addHandler(handler)
But whenever it gets to a file that contains a Chinese character, I get this error:
--- Logging error ---
Traceback (most recent call last):
File "C:\Anaconda3\lib\logging\__init__.py", line 1086, in emit
stream.write(msg + self.terminator)
File "C:\Anaconda3\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 133-135: character maps to <undefined>
Call stack:
File "C:\Anaconda3\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Anaconda3\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Anaconda3\lib\site-packages\spyder_kernels\console\__main__.py", line 23, in <module>
start.main()
File "C:\Anaconda3\lib\site-packages\spyder_kernels\console\start.py", line 328, in main
kernel.start()
File "C:\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 677, in start
self.io_loop.start()
File "C:\Anaconda3\lib\site-packages\tornado\platform\asyncio.py", line 199, in start
self.asyncio_loop.run_forever()
File "C:\Anaconda3\lib\asyncio\base_events.py", line 601, in run_forever
self._run_once()
File "C:\Anaconda3\lib\asyncio\base_events.py", line 1905, in _run_once
handle._run()
File "C:\Anaconda3\lib\asyncio\events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "C:\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 471, in dispatch_queue
await self.process_one()
File "C:\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 460, in process_one
await dispatch(*args)
File "C:\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 367, in dispatch_shell
await result
File "C:\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 662, in execute_request
reply_content = await reply_content
File "C:\Anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 360, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 532, in run_cell
return super().run_cell(*args, **kwargs)
File "C:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2863, in run_cell
result = self._run_cell(
File "C:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2909, in _run_cell
return runner(coro)
File "C:\Anaconda3\lib\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner
coro.send(None)
File "C:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3106, in run_cell_async
has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
File "C:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3309, in run_ast_nodes
if await self.run_code(code, result, async_=asy):
File "C:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3369, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "C:\Users\[redacted]\AppData\Local\Temp\ipykernel_10272\3176266708.py", line 1, in <cell line: 1>
runfile('C:/Users/[redacted]/.spyder-py3/dropbox unshare public files in folder.py', wdir='C:/Users/[redacted]/.spyder-py3')
File "C:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 577, in runfile
exec_code(file_code, filename, ns_globals, ns_locals,
File "C:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 465, in exec_code
exec(compiled, ns_globals, ns_locals)
File "C:\Users\[redacted]\.spyder-py3\dropbox unshare public files in folder.py", line 378, in <module>
unshare_public_files(folder_to_unshare)
File "C:\Users\[redacted]\.spyder-py3\dropbox unshare public files in folder.py", line 89, in unshare_public_files
find_shared_links_in_folder(folder_path)
File "C:\Users\[redacted]\.spyder-py3\dropbox unshare public files in folder.py", line 171, in find_shared_links_in_folder
find_shared_links_in_folder(filepath)
File "C:\Users\[redacted]\.spyder-py3\dropbox unshare public files in folder.py", line 171, in find_shared_links_in_folder
find_shared_links_in_folder(filepath)
File "C:\Users\[redacted]\.spyder-py3\dropbox unshare public files in folder.py", line 171, in find_shared_links_in_folder
find_shared_links_in_folder(filepath)
File "C:\Users\[redacted]\.spyder-py3\dropbox unshare public files in folder.py", line 162, in find_shared_links_in_folder
link_sharing_check(file, links)
File "C:\Users\[redacted]\.spyder-py3\dropbox unshare public files in folder.py", line 191, in link_sharing_check
logger.debug(dmsg)
Message: '[filename with Chinese characters] public'
Arguments: ()
For some reason, sometimes this triggers my exceptions that stop the entire code (in the major functions I have try/except blocks that exit the code if triggered) and sometimes it doesn't. I have been going insane trying to figure out how to make it encode using utf-8 instead of charmap - I can live with it when it's just throwing errors and continuing on its merry way, but sometimes it stops the whole thing in its tracks. It's currently running throwing errors without stopping, but I'm not even sure what changed between the last run where it stopped the whole program and this one.
I have tried setting literally everything that can be encoded as utf-8 to do so including things that have nothing to do with the logging. I even tried adding ".encode().decode('utf-8')" to the end of any filepath that was being logged but that didn't work (I didn't really expect it to...)
I am using Python 3.9 through Spyder, which I installed using Anaconda.
Simple lines of a script. It is to remove some columns from an Excel file and save it to a new file.
import pandas as pd
import numpy as np
work_file = "C:\\My Documents\\the_file.xlsx"
df = pd.read_excel(work_file, sheet_name = "Sheet1", index_col = 0)
column_list_to_remove = ["Name","Gender","Register"]
results1 = df.drop(column_list_to_remove, axis=1)
writer = pd.ExcelWriter("C:\\My Documents\\new-file.xlsx")
results1.to_excel(writer,'Sheet1')
writer.save()
It had been working well on an old computer, both small and big (thousand rows) Excel files.
I have now upgraded to a new computer with bigger RAM (16 GB). When I run this script, it was well on a small file (a few thousand lines). But when it runs on the bigger file (hundred thousand rows Excel), it gives me below error message.
How can I have this corrected? Thank you.
Error message:
Traceback (most recent call last):
File "C:\Python38\lib\xml\etree\ElementTree.py", line 832, in _get_writer
yield file.write
File "C:\Python38\lib\xml\etree\ElementTree.py", line 772, in write
serialize(write, self._root, qnames, namespaces,
File "C:\Python38\lib\xml\etree\ElementTree.py", line 937, in _serialize_xml
_serialize_xml(write, e, qnames, None,
File "C:\Python38\lib\xml\etree\ElementTree.py", line 937, in _serialize_xml
_serialize_xml(write, e, qnames, None,
File "C:\Python38\lib\xml\etree\ElementTree.py", line 937, in _serialize_xml
_serialize_xml(write, e, qnames, None,
File "C:\Python38\lib\xml\etree\ElementTree.py", line 931, in _serialize_xml
write(" %s=\"%s\"" % (qnames[k], v))
MemoryError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\My Documents\my_script.py", line 9, in <module>
writer.save()
File "C:\Python38\lib\site-packages\pandas\io\excel\_openpyxl.py", line 43, in save
return self.book.save(self.path)
File "C:\Python38\lib\site-packages\openpyxl\workbook\workbook.py", line 392, in save
save_workbook(self, filename)
File "C:\Python38\lib\site-packages\openpyxl\writer\excel.py", line 293, in save_workbook
writer.save()
File "C:\Python38\lib\site-packages\openpyxl\writer\excel.py", line 275, in save
self.write_data()
File "C:\Python38\lib\site-packages\openpyxl\writer\excel.py", line 75, in write_data
self._write_worksheets()
File "C:\Python38\lib\site-packages\openpyxl\writer\excel.py", line 215, in _write_worksheets
self.write_worksheet(ws)
File "C:\Python38\lib\site-packages\openpyxl\writer\excel.py", line 200, in write_worksheet
writer.write()
File "C:\Python38\lib\site-packages\openpyxl\worksheet\_writer.py", line 360, in write
self.close()
File "C:\Python38\lib\site-packages\openpyxl\worksheet\_writer.py", line 368, in close
self.xf.close()
File "C:\Python38\lib\site-packages\openpyxl\worksheet\_writer.py", line 299, in get_stream
pass
File "C:\Python38\lib\contextlib.py", line 120, in __exit__
next(self.gen)
File "C:\Python38\lib\site-packages\et_xmlfile\xmlfile.py", line 50, in element
self._write_element(el)
File "C:\Python38\lib\site-packages\et_xmlfile\xmlfile.py", line 77, in _write_element
xml = tostring(element)
File "C:\Python38\lib\xml\etree\ElementTree.py", line 1133, in tostring
ElementTree(element).write(stream, encoding,
File "C:\Python38\lib\xml\etree\ElementTree.py", line 772, in write
serialize(write, self._root, qnames, namespaces,
File "C:\Python38\lib\contextlib.py", line 131, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Python38\lib\xml\etree\ElementTree.py", line 832, in _get_writer
yield file.write
File "C:\Python38\lib\contextlib.py", line 525, in __exit__
raise exc_details[1]
File "C:\Python38\lib\contextlib.py", line 510, in __exit__
if cb(*exc_details):
File "C:\Python38\lib\contextlib.py", line 382, in _exit_wrapper
callback(*args, **kwds)
ValueError: I/O operation on closed file.
Replace your last three lines of code with the following:
with pd.ExcelWriter("C:\\My Documents\\new-file.xlsx") as writer:
results1.to_excel(writer)
The problem is resolved by reinstalled a 64-bit Python version. No change made to the codes.
I have a try block which raises an exception every time, in except block I used logging.exception('some message') to log error. But while handling the above exception another exception occurred.
try:
raise Exception('')
#some key error exception raised.
except Exception as e:
abortMsg = "some error"
logging.exception(abortMsg)
return
I'am pretty sure that the error is caused by the stack trace of the exception,Any operation related to it like printing it , or using traceback.print_exc() raises exceptions recursively
The error log when i used traceback.print_exc()
Error log:
--- Logging error ---
Unhandled exception in thread started by <bound method Thread._bootstrap of <Thread(Thread-5,
started 139762597885696)>>
Traceback (most recent call last):
File "somefile.py", line 570, in postProcess
reporter.write_report()
File "someotherfile.py", line 115, in write_report
File "someotherfile.py", line 295, in _addSectionResult
File "someotherfile.py", line 315, in _addCheckSummary
KeyError: 'message_zero'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/somefile.py", line 310, in execute
self.postProcess()
File "/somefile.py", line 580, in postProcess
s = str(traceback.print_exc())
File "/usr/lib/python3.5/traceback.py", line 159, in print_exc
print_exception(*sys.exc_info(), limit=limit, file=file, chain=chain)
File "/usr/lib/python3.5/traceback.py", line 100, in print_exception
type(value), value, tb, limit=limit).format(chain=chain):
File "/usr/lib/python3.5/traceback.py", line 474, in __init__
capture_locals=capture_locals)
File "/usr/lib/python3.5/traceback.py", line 358, in extract
f.line
File "/usr/lib/python3.5/traceback.py", line 282, in line
self._line = linecache.getline(self.filename, self.lineno).strip()
File "/usr/lib/python3.5/linecache.py", line 16, in getline
lines = getlines(filename, module_globals)
File "/usr/lib/python3.5/linecache.py", line 47, in getlines
return updatecache(filename, module_globals)
File "/usr/lib/python3.5/linecache.py", line 137, in updatecache
lines = fp.readlines()
File "/usr/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 4: invalid start byte'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.5/logging/__init__.py", line 980, in emit
msg = self.format(record)
File "/usr/lib/python3.5/logging/__init__.py", line 830, in format
return fmt.format(record)
File "/usr/lib/python3.5/logging/__init__.py", line 575, in format
record.exc_text = self.formatException(record.exc_info)
File "/usr/lib/python3.5/logging/__init__.py", line 525, in formatException
traceback.print_exception(ei[0], ei[1], tb, None, sio)
File "/usr/lib/python3.5/traceback.py", line 100, in print_exception
type(value), value, tb, limit=limit).format(chain=chain):
File "/usr/lib/python3.5/traceback.py", line 487, in __init__
self._load_lines()
File "/usr/lib/python3.5/traceback.py", line 500, in _load_lines
self.__context__._load_lines()
File "/usr/lib/python3.5/traceback.py", line 498, in _load_lines
frame.line
File "/usr/lib/python3.5/traceback.py", line 282, in line
self._line = linecache.getline(self.filename, self.lineno).strip()
File "/usr/lib/python3.5/linecache.py", line 16, in getline
lines = getlines(filename, module_globals)
File "/usr/lib/python3.5/linecache.py", line 47, in getlines
return updatecache(filename, module_globals)
File "/usr/lib/python3.5/linecache.py", line 137, in updatecache
lines = fp.readlines()
File "/usr/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 4: invalid start byte
Is there any way to see and analyse stack trace which is causing error?
PS : I'm using this inside a docker. While i run this file locally no recursive exception is raised.
EDIT : Any exception raised from anywhere from the code is causing recursive exception(unicode decode error)
Everything went well when all words in my file are in English, but when I use codes like "print("你好")", or even use a comment like "# 你好", then flake8 stopped working, and only display "an exception was thrown. See ALEDetail" in the first line.[I use w0rp/ale as plugin in vim80]
If I enter :ALEDetail in the vim, the information looks like below:
Traceback (most recent call last):
File "d:\programmingtool\python\python35\lib\site-packages\flake8\utils.py", line 82, in _stdin_get_value_py3
return io.StringIO(stdin_value.decode(coding))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 92: invalid start byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\programmingtool\python\python35\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "d:\programmingtool\python\python35\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\ProgrammingTool\python\python35\Scripts\flake8.exe\__main__.py", line 9, in <module>
File "d:\programmingtool\python\python35\lib\site-packages\flake8\main\cli.py", line 16, in main
app.run(argv)
File "d:\programmingtool\python\python35\lib\site-packages\flake8\main\application.py", line 396, in run
self._run(argv)
File "d:\programmingtool\python\python35\lib\site-packages\flake8\main\application.py", line 384, in _run
self.run_checks()
File "d:\programmingtool\python\python35\lib\site-packages\flake8\main\application.py", line 309, in run_checks
self.file_checker_manager.start(files)
File "d:\programmingtool\python\python35\lib\site-packages\flake8\checker.py", line 342, in start
self.make_checkers(paths)
File "d:\programmingtool\python\python35\lib\site-packages\flake8\checker.py", line 246, in make_checkers
checker for checker in checkers if checker.should_process
File "d:\programmingtool\python\python35\lib\site-packages\flake8\checker.py", line 246, in <listcomp>
checker for checker in checkers if checker.should_process
File "d:\programmingtool\python\python35\lib\site-packages\flake8\checker.py", line 243, in <genexpr>
if should_create_file_checker(filename, argument)
File "d:\programmingtool\python\python35\lib\site-packages\flake8\checker.py", line 378, in __init__
self.processor = self._make_processor()
File "d:\programmingtool\python\python35\lib\site-packages\flake8\checker.py", line 392, in _make_processor
return processor.FileProcessor(self.filename, self.options)
File "d:\programmingtool\python\python35\lib\site-packages\flake8\processor.py", line 61, in __init__
self.lines = self.read_lines()
File "d:\programmingtool\python\python35\lib\site-packages\flake8\processor.py", line 297, in read_lines
lines = self.read_lines_from_stdin()
File "d:\programmingtool\python\python35\lib\site-packages\flake8\processor.py", line 333, in read_lines_from_stdin
return utils.stdin_get_value().splitlines(True)
File "d:\programmingtool\python\python35\lib\site-packages\flake8\utils.py", line 95, in stdin_get_value
stdin_value = _stdin_get_value_py3()
File "d:\programmingtool\python\python35\lib\site-packages\flake8\utils.py", line 84, in _stdin_get_value_py3
return io.StringIO(stdin_value.decode('utf-8'))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 92: invalid start byte
When I run my application, which finds links, checks if they exist in the database, and adds them to the database, I get an error.
Traceback (most recent call last):
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~nothing-but-net/1.360769209191920043/main.py", line 460, in get
blchrlinks(True, a)
File "/base/data/home/apps/s~nothing-but-net/1.360769209191920043/main.py", line 271, in blchrlinks
if Articles.by_name(title):
File "/base/data/home/apps/s~nothing-but-net/1.360769209191920043/main.py", line 498, in by_name
u = Articles.all().filter("name =", name).get()
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2099, in get
results = self.run(limit=1, **kwargs)
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2063, in run
iterator = raw_query.Run(**kwargs)
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 1622, in Run
itr = Iterator(self.GetBatcher(config=config))
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 1601, in GetBatcher
return self.GetQuery().run(_GetConnection(), query_options)
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 1490, in GetQuery
filter_predicate=self.GetFilterPredicate(),
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 1534, in GetFilterPredicate
property_filters.append(datastore_query.make_filter(name, op, values))
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_query.py", line 107, in make_filter
properties = datastore_types.ToPropertyPb(name, values)
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore_types.py", line 1745, in ToPropertyPb
pbvalue = pack_prop(name, v, pb.mutable_value())
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore_types.py", line 1556, in PackString
pbvalue.set_stringvalue(unicode(value).encode('utf-8'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 57: ordinal not in range(128)
My code for by_name is:
def by_name(cls, name):
u = Articles.all().filter("name =", name).get()
return u
As the last line of the stack trace shows, it's trying to convert the value to unicode first and then encoding it as utf-8. However, the (implicit) conversion is using ascii, which is not enough for your string. You could try converting it to unicode yourself, using the right encoding, before passing it to filter. Example:
u = Articles.all().filter("name =", name.decode('utf-8')).get()
(remember that you need to provide the correct encoding; if name is not a UTF-8 string, but a Cp1252, ISO-Latin or something else, you need to specify that in the decode call)