Skip to content
Advertisement

Tag: python-3.x

How to strip all whitespace from string

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

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

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.

Advertisement