How do I strip all the spaces in a python string? For example, I want a string like strip my spaces to be turned into stripmyspaces, but I cannot seem to accomplish that with strip(): Answer Taking advantage of str.split’s behavior with no sep parameter: If you just want to remove spaces instead of all whitespace: Premature optimization Even though
Tag: python-3.x
Pinging servers in Python
In Python, is there a way to ping a server through ICMP and return TRUE if the server responds, or FALSE if there is no response? Answer This function works in any OS (Unix, Linux, macOS, and Windows) Python 2 and Python 3 EDITS: By @radato os.system was replaced by subprocess.call. This avoids shell injection vulnerability in cases where your
Can’t download YouTube video
I’m having trouble retrieving the YouTube video automatically. Here’s the code. The problem is the last part. download = urllib.request.urlopen(download_url).read() There’s an error message (thanks Wooble): Answer The code on the original question relies on several assumptions about the content of YouTube pages and URLs (expressed in constructs such as “url.split(‘something=’)[1]”) which may not always be true. I tested it
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
String formatting
I have a list filter = [‘a’, ‘b’, ‘c’]. I need to frame the following string out of the list “item -a item -b item -c”. Which is the most efficient way to do this? Usually the list filter contains 100 to 200 items and each would be of length 100 – 150. Wouldn’t that lead to overflow? And what
Why does integer division yield a float instead of another integer?
Consider this division in Python 3: Is this intended? I strongly remember earlier versions returning int/int = int. What should I do? Is there a new division operator or must I always cast? In 2.x, the behaviour was indeed reversed; see How can I force division to be floating point? Division keeps rounding down to 0? for the opposite, 2.x-specific
How do I use raw_input in Python 3?
In Python 2: In Python 3, I get an error: NameError: name ‘raw_input’ is not defined Answer Starting with Python 3, raw_input() was renamed to input(). From What’s New In Python 3.0, Builtins section second item.
What is the best way to remove accents (normalize) in a Python unicode string?
I have a Unicode string in Python, and I would like to remove all the accents (diacritics). I found on the web an elegant way to do this (in Java): convert the Unicode string to its long normalized form (with a separate character for letters and diacritics) remove all the characters whose Unicode type is “diacritic”. Do I need to