Can’t get Python to import from a different folder

I believe you need to create a file called __init__.py in the Models directory so that python treats it as a module. Then you can do: You can include code in the __init__.py (for instance initialization code that a few different classes need) or leave it blank. But it must be there.

Beginner Python: Reading and writing to the same file

Updated Response: This seems like a bug specific to Windows – http://bugs.python.org/issue1521491. Quoting from the workaround explained at http://mail.python.org/pipermail/python-bugs-list/2005-August/029886.html the effect of mixing reads with writes on a file open for update is entirely undefined unless a file-positioning operation occurs between them (for example, a seek()). I can’t guess what you expect to happen, but seems most … Read more

Rotating a two-dimensional array in Python

Consider the following two-dimensional list: Lets break it down step by step: This list is passed into zip() using argument unpacking, so the zip call ends up being the equivalent of this: Hopefully the comments make it clear what zip does, it will group elements from each input iterable based on index, or in other words it groups the columns.

Strange error about invalid syntax

Getting “invalid syntax” on a plain return statement is pretty much impossible. If you use it outside of a function, you get ‘return’ outside function, if you have the wrong indentation you get IndentationError, etc. The only way I can get a SyntaxError: invalid syntax on a return statement, is if in fact it doesn’t say return at all, but if … Read more

What does this Django regular expression mean? `?P`

In django, named capturing groups are passed to your view as keyword arguments. Unnamed capturing groups (just a parenthesis) are passed to your view as arguments. The ?P is a named capturing group, as opposed to an unnamed capturing group. http://docs.python.org/library/re.html (?P<name>…) Similar to regular parentheses, but the substring matched by the group is accessible within … Read more

How to parse tsv file with python?

Just use the csv module. It knows about all the possible corner cases in CSV files like new lines in quoted fields. And it can delimit on tabs. will correctly output: