Unicodeencodeerror Latin 1

Unicodeencodeerror Latin 1




🛑 ALL INFORMATION CLICK HERE 👈🏻👈🏻👈🏻

































Unicodeencodeerror Latin 1

Sign up or log in to customize your list.

more stack exchange communities

company blog


Stack Overflow for Teams
– Start collaborating and sharing organizational knowledge.



Create a free Team
Why Teams?



Asked
3 years, 3 months ago


Modified
1 year, 1 month ago


7,698 23 23 gold badges 86 86 silver badges 194 194 bronze badges



Sorted by:


Reset to default





Highest score (default)


Trending (recent votes count more)


Date modified (newest first)


Date created (oldest first)




28.6k 7 7 gold badges 74 74 silver badges 103 103 bronze badges


28.6k 7 7 gold badges 74 74 silver badges 103 103 bronze badges


196 1 1 silver badge 11 11 bronze badges


Not the answer you're looking for? Browse other questions tagged python pdf unicode fpdf python-3.7 or ask your own question .

Stack Overflow

Questions
Help



Products

Teams
Advertising
Collectives
Talent



Company

About
Press
Work Here
Legal
Privacy Policy
Terms of Service
Contact Us
Cookie Settings
Cookie Policy



Stack Exchange Network



Technology




Culture & recreation




Life & arts




Science




Professional




Business





API





Data






Accept all cookies



Customize settings


Find centralized, trusted content and collaborate around the technologies you use most.
Connect and share knowledge within a single location that is structured and easy to search.
New! Save questions or answers and organize your favorite content. Learn more .
I am having an issue with Unicode with a variable contents when writing to a .pdf with python.
Which is it getting caught on an em dash basically.
I have tried taking that variable, where the contents has an 'em dash' and redefined it with an ' .encode('utf-8') ' for example, i.e., below:
Below is my full code, how could I simply fix my Unicode error in ' Body ' variable contents.
Converting to utf-8 or western , anything outside of ' latin-1 '. Any suggestions?
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 will be free of any non-latin-1 characters. However, some chars may be replaced with ?
The reason for this error is that you are trying to render a character in your PDF that is outside the code range of latin-1 encoding. FPDF uses latin-1 as default encoding for all its build-in fonts.
So as a workaround you can just remove all characters from your text that do not fit into latin-1 encoding. (see my other answer for this workaround).
To fix this error and be able to render those characters in your PDF you need to use fonts that support a wider range of characters. To address this the FPDF library supports Unicode font.
For example you could get the free Google Noto fonts , which support a wide range of Unicode endpoints. For most western languages I would recommend the NotoSans font set. But you can also get fonts for many other languages and scripts including Chinese, Hebrew or Arabic.
Here is how to enable the Unicode fonts in your code for FPDF:
First you need to tell FPDF library where it can find the font files. In this example I am setting it to the sub-folder fonts of the current folder.
Then you need to add the fonts to your PDF document. In this example I am adding the NotoSans fonts for the styles normal, bold, italic and bold-italic:
Now you can use the new fonts normally in your PDF document with set_font() . Here is an example for normal text:
You can also change the encoding through the .set_doc_option() method (documentation here ). I tried Erik's method, which worked for me, but then after adding some more complexities (such as a second PDF and using the write_html() method which required creating a new class), I went back to having the same error. Changing the encoding for the whole document should solve the overall problem as you said.
The readthedocs page says you can only use latin-1 or windows-1252, but pdf.set_doc_option('core_fonts_encoding', 'utf-8') worked for me according to the debugger. Just be aware that some characters will need fixing, like the apostrophe (') showing as â€ÂTM in the PDF.
Hope this is the global fix for this issue you were looking for, even if several months late!
I was trying Erik's solution with some changes, works great with a mix of English and Arabic text. Sample code posted below to generate PDF using pyFPDF.
Thanks for contributing an answer to Stack Overflow!

By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2022.10.14.21376


By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .



By
admin

|
October 27, 2021


Thank you for visiting. You can now buy me a coffee!
We you are using python to crawl a web page, you may get this error: UnicodeEncodeError: ‘latin-1’ codec can’t encode character ‘\u2026’ . In this tutorial, we will introduce you how to fix it.
As to us, you can check your http request header.
We can find there is a string … in our http request header.
We remove it and find this error is fixed.
Your email address will not be published. Required fields are marked *
Save my name, email, and website in this browser for the next time I comment.




Wiki
Books
Shop
Courses
Careers



Python.Engineering

Wiki

UnicodeEncodeError: “latin-1” codec can”t encode character

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

What could be causing this error when I try to insert a foreign character into the database?

👻 Read also: what is the best laptop for engineering students ?

How to insert newlines on argparse help text?
I"m using argparse in Python 2.7 for parsing input options. One of my options is a multiple choice. I want to make a list in its help text, e.g.
However, argparse strips all newlines and consecutive spaces. The result looks like
How to insert newlines in the help text?
Is a Python list guaranteed to have its elements stay in the order they are inserted in?
If I have the following Python code
Will x be guaranteed to always be [1,2,3] , or are other orderings of the interim elements possible?
Yes, the order of elements in a python list is persistent.
Inserting image into IPython notebook markdown
I am starting to depend heavily on the IPython notebook app to develop and document algorithms. It is awesome; but there is something that seems like it should be possible, but I can"t figure out how to do it:
I would like to insert a local image into my (local) IPython notebook markdown to aid in documenting an algorithm. I know enough to add something like to the markdown, but that is about as far as my knowledge goes. I assume I could put the image in the directory represented by 127.0.0.1:8888 (or some subdirectory) to be able to access it, but I can"t figure out where that directory is. (I"m working on a mac.) So, is it possible to do what I"m trying to do without too much trouble?
Most of the answers given so far go in the wrong direction, suggesting to load additional libraries and use the code instead of markup. In Ipython/Jupyter Notebooks it is very simple. Make sure the cell is indeed in markup and to display a image use:
Further advantage compared to the other methods proposed is that you can display all common file formats including jpg, png, and gif (animations).
How do I merge two dictionaries in a single expression (taking union of dictionaries)?
I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged (i.e. taking the union). The update() method would be what I need, if it returned its result instead of modifying a dictionary in-place.
How can I get that final merged dictionary in z , not x ?
(To be extra-clear, the last-one-wins conflict-handling of dict.update() is what I"m looking for as well.)
For dictionaries x and y , z becomes a shallowly-merged dictionary with values from y replacing those from x .
In Python 3.9.0 or greater (released 17 October 2020): PEP-584 , discussed here , was implemented and provides the simplest method:
In Python 2, (or 3.4 or lower) write a function:
Say you have two dictionaries and you want to merge them into a new dictionary without altering the original dictionaries:
The desired result is to get a new dictionary ( z ) with the values merged, and the second dictionary"s values overwriting those from the first.
A new syntax for this, proposed in PEP 448 and available as of Python 3.5 , is
And it is indeed a single expression.
Note that we can merge in with literal notation as well:
It is now showing as implemented in the release schedule for 3.5, PEP 478 , and it has now made its way into the What"s New in Python 3.5 document.
However, since many organizations are still on Python 2, you may wish to do this in a backward-compatible way. The classically Pythonic way, available in Python 2 and Python 3.0-3.4, is to do this as a two-step process:
In both approaches, y will come second and its values will replace x "s values, thus b will point to 3 in our final result.
If you are not yet on Python 3.5 or need to write backward-compatible code, and you want this in a single expression , the most performant while the correct approach is to put it in a function:
and then you have a single expression:
You can also make a function to merge an arbitrary number of dictionaries, from zero to a very large number:
This function will work in Python 2 and 3 for all dictionaries. e.g. given dictionaries a to g :
and key-value pairs in g will take precedence over dictionaries a to f , and so on.
Don"t use what you see in the formerly accepted answer:
In Python 2, you create two lists in memory for each dict, create a third list in memory with length equal to the length of the first two put together, and then discard all three lists to create the dict. In Python 3, this will fail because you"re adding two dict_items objects together, not two lists -
and you would have to explicitly create them as lists, e.g. z = dict(list(x.items()) + list(y.items())) . This is a waste of resources and computation power.
Similarly, taking the union of items() in Python 3 ( viewitems() in Python 2.7) will also fail when values are unhashable objects (like lists, for example). Even if your values are hashable, since sets are semantically unordered, the behavior is undefined in regards to precedence. So don"t do this:
This example demonstrates what happens when values are unhashable:
Here"s an example where y should have precedence, but instead the value from x is retained due to the arbitrary order of sets:
This uses the dict constructor and is very fast and memory-efficient (even slightly more so than our two-step process) but unless you know precisely what is happening here (that is, the second dict is being passed as keyword arguments to the dict constructor), it"s difficult to read, it"s not the intended usage, and so it is not Pythonic.
Here"s an example of the usage being remediated in django .
Dictionaries are intended to take hashable keys (e.g. frozenset s or tuples), but this method fails in Python 3 when keys are not strings.
From the mailing list , Guido van Rossum, the creator of the language, wrote:
I am fine with
declaring dict({}, **{1:3}) illegal, since after all it is abuse of
the ** mechanism.
Apparently dict(x, **y) is going around as "cool hack" for "call
x.update(y) and return x". Personally, I find it more despicable than
cool.
It is my understanding (as well as the understanding of the creator of the language ) that the intended usage for dict(**y) is for creating dictionaries for readability purposes, e.g.:
Despite what Guido says, dict(x, **y) is in line with the dict specification, which btw. works for both Python 2 and 3. The fact that this only works for string keys is a direct consequence of how keyword parameters work and not a short-coming of dict. Nor is using the ** operator in this place an abuse of the mechanism, in fact, ** was designed precisely to pass dictionaries as keywords.
Again, it doesn"t work for 3 when keys are not strings. The implicit calling contract is that namespaces take ordinary dictionaries, while users must only pass keyword arguments that are strings. All other callables enforced it. dict broke this consistency in Python 2:
This inconsistency was bad given other implementations of Python (PyPy, Jython, IronPython). Thus it was fixed in Python 3, as this usage could be a breaking change.
I submit to you that it is malicious incompetence to intentionally write code that only works in one version of a language or that only works given certain arbitrary constraints.
dict(x.items() + y.items()) is still the most readable solution for Python 2. Readability counts.
My response: merge_two_dicts(x, y) actually seems much clearer to me, if we"re actually concerned about readability. And it is not forward compatible, as Python 2 is increasingly deprecated.
{**x, **y} does not seem to handle nested dictionaries. the contents of nested keys are simply overwritten, not merged [...] I ended up being burnt by these answers that do not merge recursively and I was surprised no one mentioned it. In my interpretation of the word "merging" these answers describe "updating one dict with another", and not merging.
Yes. I must refer you back to the question, which is asking for a shallow merge of two dictionaries, with the first"s values being overwritten by the second"s - in a single expression.
Assuming two dictionaries of dictionaries, one might recursively merge them in a single function, but you should be careful not to modify the dictionaries from either source, and the surest way to avoid that is to make a copy when assigning values. As keys must be hashable and are usually therefore immutable, it is pointless to copy them:
Coming up with contingencies for other value types is far beyond the scope of this question, so I will point you at my answer to the canonical question on a "Dictionaries of dictionaries merge" .
These approaches are less performant, but they will provide correct behavior.
They will be much less performant than copy and update or the new unpacking because they iterate through each key-value pair at a higher level of abstraction, but they do respect the order of precedence (latter dictionaries have precedence)
You can also chain the dictionaries manually inside a dict comprehension :
or in Python 2.6 (and perhaps as early as 2.4 when generator expressions were introduced):
itertools.chain will chain the iterators over the key-value pairs in the correct order:
I"m only going to do the performance analysis of the usages known to behave correctly. (Self-contained so you can copy and paste yourself.)
This will, as you want it, put the final dict in z , and make the value for key b be properly overridden by the second ( y ) dict"s value:
If you use Python 2, you can even remove the list() calls. To create z:
If you use Python version 3.9.0a4 or greater, then you can directly use:
Best Python online courses for 2022
psycopg2: insert multiple rows with one query
How to convert Nonetype to int or string?
How to specify multiple return types using type-hints
Javascript Error: IPython is not defined in JupyterLab
Python OpenCV | cv2.putText () method
Python | os.path.realpath () method
Python OpenCV | cv2.circle () method
Python OpenCV cv2.cvtColor () method
Python - Move item to the end of the list
time.perf_counter () function in Python
Check if one list is a subset of another in Python
© 2017—2022 Python Engineering Hub

EN | ES | DE | FR | IT | RU | TR | PL | PT | JP | KR | CN | HI | NL



Aching Ball Handjobs 6
Nude Nude Nude Milf Gilf
Grandma S Porno

Report Page