Removing entries from a dictionary containing bad words

I have a dictionary with each item containing a quote in another dictionary: { ‘example1’: {‘quote’: ‘And it must follow, as the night the day, thou canst not then be false to any man.\n’}, ‘example2’: {‘quote’: ‘What a piece of work is man! how noble in reason!.\n’} } I need to completely remove each entry … Read more

A good way to make long strings wrap to newline?

In my project, I have a bunch of strings that are read in from a file. Most of them, when printed in the command console, exceed 80 characters in length and wrap around, looking ugly. I want to be able to have Python read the string, then test if it is over 75 characters in … Read more

How can I get 2.x-like sorting behaviour in Python 3.x?

I’m trying to replicate (and if possible improve on) Python 2.x’s sorting behaviour in 3.x, so that mutually orderable types like int, float etc. are sorted as expected, and mutually unorderable types are grouped within the output. Here’s an example of what I’m talking about: >>> sorted([0, ‘one’, 2.3, ‘four’, -5]) # Python 2.x [-5, … Read more

Why can’t I use the method __cmp__ in Python 3 as for Python 2?

The following piece of code class point: def __init__(self, x, y): self.x = x self.y = y def dispc(self): return (‘(‘ + str(self.x) + ‘,’ + str(self.y) + ‘)’) def __cmp__(self, other): return ((self.x > other.x) and (self.y > other.y)) works fine in Python 2, but in Python 3 I get an error: >>> p=point(2,3) … Read more

How to reinstall python@2 from Homebrew?

I have been having issues with openssl and python@2 with brew, which have explained here (unresolved). The documented workaround to reinstall Python and openssl was not working, so I decided I would uninstall and reinstall Python. The problem is, when you try to install Python 2 with brew, you receive this message: brew install python@2 … Read more

SQLite, python, unicode, and non-utf data

I started by trying to store strings in sqlite using python, and got the message: sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings. Ok, I switched to Unicode … Read more