UnicodeEncodeError: 'latin-1' codec can't encode character
Character U+201C Left Double Quotation Mark is not present in the Latin-1 (ISO-8859-1) encoding.
It is present in code page 1252 (Western European). This is a Windows-specific encoding that is based on ISO-8859-1 but which puts extra characters into the range 0x80-0x9F. Code page 1252 is often confused with ISO-8859-1, and it's an annoying but now-standard web browser behaviour that if you serve your pages as ISO-8859-1, the browser will treat them as cp1252 instead. However, they really are two distinct encodings:
>>> u'He said \u201CHello\u201D'.encode('iso-8859-1')
UnicodeEncodeError
>>> u'He said \u201CHello\u201D'.encode('cp1252')
'He said \x93Hello\x94'
If you are using your database only as a byte store, you can use cp1252 to encode “
and other characters present in the Windows Western code page. But still other Unicode characters which are not present in cp1252 will cause errors.
You can use encode(..., 'ignore')
to suppress the errors by getting rid of the characters, but really in this century you should be using UTF-8 in both your database and your pages. This encoding allows any character to be used. You should also ideally tell MySQL you are using UTF-8 strings (by setting the database connection and the collation on string columns), so it can get case-insensitive comparison and sorting right.
latin-1' codec can't encode characters
Try adding this after the line where you create the data variable before you post the request
data=data.encode() #will produce bytes object encoded with utf-8
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2013' (writing to PDF)
A workaround is to convert all text to latin-1 encoding before passing it on to the library. You can do that with the following command:
text2 = text.encode('latin-1', 'replace').decode('latin-1')
text2
will be free of any non-latin-1 characters. However, some chars may be replaced with ?
fpdf UnicodeEncodeError: 'latin-1' codec can't encode character '\u2013' in position 88: ordinal not in range(256)
All the standard fonts in fpdf use latin-1
encoding. If you want to write characters that aren't in the latin-1
set, you'll need to use set_font
to specify an external font.
Reference:
https://pyfpdf.readthedocs.io/en/latest/reference/set_font/index.html
Otherwise you'll have to convert your string to latin-1
(using the encode
method) and specify whether to ignore or replace the bad characters (i.e. the ones that don't exist in latin-1
).
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2019'
I solved this by changing the font. The original font (Arial only allowed latin-1.
Related Topics
How to Force a List to a Fixed Size
How to Write Data into CSV Format as String (Not File)
<Django Object > Is Not JSON Serializable
How to Add Conda Environment to Jupyter Lab
How to Plot a Confusion Matrix
Python Super() Raises Typeerror
Solving Embarassingly Parallel Problems Using Python Multiprocessing
Is Close() Necessary When Using Iterator on a Python File Object
Python Requests Ssl Error - Certificate Verify Failed
Interactive Pixel Information of an Image in Python
Return and Yield in the Same Function
How to Call an External Program in Python and Retrieve the Output and Return Code
How to Remove the First Item from a List
Checking File Permissions in Linux with Python
How to Install Pip3 on Windows
Removing Elements from a List Containing Specific Characters