How to rearrange print output in Python from column to a row?

270 views Asked by At

I wrote a method computing Robert Haralick image features (using Mahotas package) on .tif medical images in the working directory. I receive the print output in a column like:

567657657
788979877
787879787

I need to convert it to the horizontal row appearance like: 1789789 8909888 898098098 and, if possible, append the name of the processed file before that row of output (like: imagename 7897987 90890898...). Here is the code:

def haralick(self):
   for filename in glob.iglob ('*.tif'):
      imgg = mahotas.imread (filename)
      cancertwo = mahotas.features.haralick (imgg)
      arr = numpy.array([cancertwo])
      for x in arr:
          for y in arr:
              for h in y:
                  for z in h:
                      print '%f' %z
                      print('\n')
2

There are 2 answers

2
RisingSun On

I have no experience with Python; If you mean however to print the same numbers consecutive to console or file or whatever, it will be just a matter of not printing the \n\r character but substituting them with for example a tab \t or a comma ',',

0
tom10 On

Python 2.x versions do an automatic newline after a print statement, but you can stop it from doing this with a , (and with , you get an automatic space).

In your code just use the single line (without an explicit \n):

print "%i" %z,

(I assume you're using a 2.x version for access to a full scientific stack for medical image processing, but please mention it if you're not.)