Work around “iteration over a 0-d array” when reading txt files with Python

I am using Numpy to read some txt files that could be any lines and then iterate them. I realize that I got “iteration over a 0-d array” when the file happens to contain only 1 line. Here is how I work around it — not very clever but it works.

import numpy
f = numpy.genfromtxt('test.file', dtype='str')
if f.size > 1:
   for i in f:
      print "many lines..."
else:
   i = f.datap[:]
   print "only 1 line"

And yeah, I will try to fight with my laziness and populate this blog with bits from my research that could help others. With the help of Google, I benefit a lot from people I don’t know, and now I want to give.