UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128)
The file is being read as a bunch of str
s, but it should be unicode
s. Python tries to implicitly convert, but fails. Change:
job_titles = [line.strip() for line in title_file.readlines()]
to explicitly decode the str
s to unicode
(here assuming UTF-8):
job_titles = [line.decode('utf-8').strip() for line in title_file.readlines()]
It could also be solved by importing the codecs
module and using codecs.open
rather than the built-in open
.
Python 3 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)
You should probably give encoding for pickle.load(f, encoding='latin1')
, but please make sure all the characters in your file will follow the encoding.
By default, your pickle code is trying to decode the file with 'ASCII' which fails. Instead you can explicitly tell which one to use. See this from Documentation.
If latin1 doesn't solve, try with encoding='bytes'
and then decode all the keys and values later on.
how to interpret this error UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 164: ordinal not in range(128)
found a way to solve this:
f = open(file, encoding = 'utf-8', mode = "r+")
f = open(file, encoding = 'utf-8', mode = "w")
it worked.
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 4: ordinal not in range(128)
I think what you are looking for is:
# ...
text = articleFile.read().decode('utf-8')
keyphrases = extractKeyphrases(text)
# ...
Basicly you want to decode to an unicode string the contents of the file as soon as you read it. Then the rest of your program is save from conversion problems. Please also make sure the file is actually in utf-8 encoding. If unsure try latin1 as encoding because that will never throw an exception while decoding (but still produces wrong text of course when the file is not in latin1 encoding)
Python UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 ordinal not in range(128)
You should really use a proper templating system. Jinja2 is included with AppEngine.
However in the meantime your problem is that your templates are ASCII but your data is not (can't tell if it's utf-8 or unicode). Easy solution is to prefix each template string with u
to make it Unicode.
But, you should really use a proper templating system.
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 1: ordinal not in range(128)
So, I managed to solve my issue.
- I figured out that
active_agents.values(...."first_name", "last_name").order_by('-total_paid_transaction_value_last_month')
retrieved a dictionary where its key and values were already in unicode (bacause of the way it was configured in models.py, django 1.11 and python2.7. So, the process of serializing was just fine.
It is indeed true that the final result that went to template was looking like’C\xc4\x83t\xc4\x83lin'
. The error came from /xc4/. - In order to fix it on template, I just did this:
{{ agent.full_name.decode("utf-8") }}, which gave me the right result:Cătălin Pintea
Thanks @BoarGules. It was true that d['last_name']
and d['first_name']
were in unicode. So when I did the concatenation, I had to add u" "
.
Related Topics
Get the Position of the Largest Value in a Multi-Dimensional Numpy Array
Python Ftp Implicit Tls Connection Issue
Why Return Notimplemented Instead of Raising Notimplementederror
Saving Upload in Flask Only Saves to Project Root
Pandas Sum by Groupby, But Exclude Certain Columns
Use a Library Locally Instead of Installing It
Django Rest Framework Serializing Many to Many Field
Datetime from String in Python, Best-Guessing String Format
Receive and Send Emails in Python
Difference Between the Built-In Pow() and Math.Pow() for Floats, in Python
Why Use Os.Path.Join Over String Concatenation
What Is the Point of Setlevel in a Python Logging Handler
Access Memory Address in Python
Using Print() (The Function Version) in Python2.X
Python Script for Django App to Access Models Without Using Manage.Py Shell
Failedpreconditionerror: Attempting to Use Uninitialized in Tensorflow
Execute a Function After Flask Returns Response
Replace Column Values in One Dataframe by Values of Another Dataframe