I want to display: 49 as 49.00 and: 54.9 as 54.90 Regardless of the length of the decimal or whether there are are any decimal places, I would like to display a Decimal with 2 decimal places, and I’d like to do it in an efficient way. The purpose is to display money values. eg, 4898489.00 For the analogous issue
How to make a 3D scatter plot in matplotlib
I am currently have a nx3 matrix array. I want plot the three columns as three axis’s. How can I do that? I have googled and people suggested using Matlab, but I am really having a hard time with understanding it. I also need it be a scatter plot. Can someone teach me? Answer You can use matplotlib for this.
Center-/middle-align text with PIL?
How would I center-align (and middle-vertical-align) text when using PIL? Answer Deprecation Warning: textsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use textbbox or textlength instead. Code using textbbox instead of textsize. Result Use Draw.textsize method to calculate text size and re-calculate position accordingly. Here is an example: and the result: If your fontsize is different, include
Convert NumPy array to Python list
How do I convert a NumPy array into a Python List? Answer Use tolist(): Note that this converts the values from whatever numpy type they may have (e.g. np.int32 or np.float32) to the “nearest compatible Python type” (in a list). If you want to preserve the numpy data types, you could call list() on your array instead, and you’ll end
Python Datatype for a fixed-length FIFO
I would like to know if there is a native datatype in Python that acts like a fixed-length FIFO buffer. For example, I want do create a length-5 FIFO buffer that is initialized with all zeros. Then, it might look like this: [0,0,0,0,0] Then, when I call the put function on the object, it will shift off the last zero
Django TemplateDoesNotExist?
My local machine is running Python 2.5 and Nginx on Ubuntu 8.10, with Django builded from latest development trunk. For every URL I request, it throws: TemplateDoesNotExist at /appname/path appname/template_name.html Django tried loading these templates, in this order: * Using loader django.template.loaders.filesystem.function: * Using loader django.template.loaders.app_directories.function: TEMPLATE_DIRS (‘/usr/lib/python2.5/site-packages/projectname/templates’,) Is it looking for /usr/lib/python2.5/site-packages/projectname/templates/appname/template_name.html in this case? The weird thing is
How to completely sanitize a string of illegal characters in python?
I have a feature of my program where the user can upload a csv file, which my program goes through and uses as input. I have one user complaining about a problem where his input is throwing up an error. The error is caused by there being an illegal character that is encoded wrong. The characters is below: Sometimes it
How to split an integer into a list of digits?
Suppose I have an input integer 12345. How can I split it into a list like [1, 2, 3, 4, 5]? Answer Convert the number to a string so you can iterate over it, then convert each digit (character) back to an int inside a list-comprehension:
How to make a Tkinter window jump to the front?
How do I get a Tkinter application to jump to the front? Currently, the window appears behind all my other windows and doesn’t get focus. Is there some method I should be calling? Answer Assuming you mean your application windows when you say “my other windows”, you can use the lift() method on a Toplevel or Tk: If you want
Checking File Permissions in Linux with Python
I’m writing a script to check permissions of files in user’s directories and if they’re not acceptable I’ll be warning them, but I want to check permissions of not just the logged in user, but also group and others. How can i do this? It seems to me that os.access() in Python can only check the permissions for the user