The following two code pieces, when placed in the REPL by themselves, work properly:
1)
with open('/shared/errorStatsByDowAndHour.tsv','r') as f:
for line in f:
phs.append([twodec(l.strip()) for l in line.split('\t')])
for p in phs:
print p
['1-00', '34550', 38493.75, '42154', 2745.61]
['1-01', '24087', 32319.5, '42742', 6985.69]
['1-02', '13853', 20238.25, '27358', 5407.19]
['1-03', '14686', 20409.0, '27999', 4798.54]
..
However, when combined as follows:
with open('/shared/errorStatsByDowAndHour.tsv','r') as f:
for line in f:
phs.append([twodec(l.strip()) for l in line.split('\t')])
for p in phs:
print p
The following error is generated:
>>> with open('/shared/errorStatsByDowAndHour.tsv','r') as f:
... for line in f:
... phs.append([twodec(l.strip()) for l in line.split('\t')])
... for p in phs:
File "<stdin>", line 4
for p in phs:
^
SyntaxError: invalid syntax
>>> print p
File "<stdin>", line 1
print p
^
IndentationError: unexpected indent
What is the explanation?
Whenever you drop back to top-level after an indented block in the interactive interpreter, you need to input a blank line to indicate that Python can now execute the block.
If your goal is to be able to copy/paste code from a script to the interactive interpreter and have it run, you can do the following:
exec r'''
[copy/paste your code here]
'''
Make sure to inspect the code for docstrings and other stuff that could mess this up first, though. You may be able to switch the quote type to make it work.