How do I make multi-line comments? Most languages have block comment symbols like: Answer You can use triple-quoted strings. When they’re not a docstring (the first thing in a class/function/module), they are ignored. (Make sure to indent the leading ”’ appropriately to avoid an IndentationE…
Tag: python
Converting xml to dictionary using ElementTree
I’m looking for an XML to dictionary parser using ElementTree, I already found some but they are excluding the attributes, and in my case I have a lot of attributes. Answer Call as This works as long as you don’t actually have an attribute text; if you do, then change the third line in the functio…
sqlite3.OperationalError: unable to open database file
I get this error when setting up a server in Django. It is sqlite3 which means it should create the .db file but it doesn’t seem to be doing so. I’ve stipulated SQLite as the backend and an absolute file path for where to put it, but no luck. Is this a bug or am I doing something incorrect? (Was
Python split string in moving window
I have a string with digits like so – digit = “7316717” Now I want to split the string in such a way that the output is a moving window of 3 digits at a time. So I get – [“731”, “316”, “167”, “671”, “717”] How would the approa…
A python web framework for google app engine
(Please note that this question and some of the answers are old) I want to use an existing python framework to develop an application on google appengine. It should be quick and easy to start and support test driven development practices in an easy way. Can you recommend a stack? What about django? Additional…
How to to read a matrix from a given file?
I have a text file which contains matrix of N * M dimensions. For example the input.txt file contains the following: I need to write python script where in I can import the matrix. My current python script is: the output list comes like this I need to fetch the values in int form . If I try to type
Maximum and Minimum values for ints
How do I represent minimum and maximum values for integers in Python? In Java, we have Integer.MIN_VALUE and Integer.MAX_VALUE. See also: What is the maximum float in Python?. Answer Python 3 In Python 3, this question doesn’t apply. The plain int type is unbounded. However, you might actually be lookin…
Format a datetime into a string with milliseconds
How can I format a datetime object as a string with milliseconds? Answer To get a date string with milliseconds, use [:-3] to trim the last three digits of %f (microseconds): Or slightly shorter:
How to calculate the angle between a line and the horizontal axis?
In a programming language (Python, C#, etc) I need to determine how to calculate the angle between a line and the horizontal axis? I think an image describes best what I want: Given (P1x,P1y) and (P2x,P2y) what is the best way to calculate this angle? The origin is in the topleft and only the positive quadran…
Best way to convert string to bytes in Python 3?
TypeError: ‘str’ does not support the buffer interface suggests two possible methods to convert a string to bytes: Which method is more Pythonic? Answer If you look at the docs for bytes, it points you to bytearray: bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearr…