Help with columns formatting

Thread Solved

Join Date: Mar 2008
Posts: 4
Reputation: gianniszaf is an unknown quantity at this point 
Solved Threads: 0
gianniszaf gianniszaf is offline Offline
Newbie Poster

Help with columns formatting

 
0
  #1
Jul 6th, 2009
Hi there,

I have the following string which is tab seperated

  1. dasj dhsahdwe dhasdhajks ewqhehwq dsajkdhas
  2. edward das dsaw das daswf
  3. fjdk ewf jken dsajkw dskdw
  4. hklt ewq vn1 daskcn daskw

How can I format it in python in order to make each column to have the width of the longest cell in this column? And look like below

  1. dasj dhsahdwe dhasdhajks ewqhehwq dsajkdhas
  2. edward das dsaw das daswf
  3. fjdk ewf jken dsajkw dskdw
  4. hklt ewq vn1 daskcn daskw

Thanks a lot.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,542
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 171
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Help with columns formatting

 
0
  #2
Jul 6th, 2009
One way to do this is to pad each word with spaces at the end to make the words equal in length (max length + 2):
  1. s = """\
  2. dasj dhsahdwe dhasdhajks ewqhehwq dsajkdhas
  3. edward das dsaw das daswf
  4. fjdk ewf jken dsajkw dskdw
  5. hklt ewq vn1 daskcn daskw
  6. """
  7.  
  8. # create a word list
  9. mylist = s.split()
  10. #print mylist
  11.  
  12. # find maximum word length
  13. length = 0
  14. for w in mylist:
  15. size = len(w)
  16. if size > length:
  17. length = size
  18.  
  19. #print length
  20.  
  21. # pad spaces to the end of each word
  22. # add a newline to every fifth word
  23. newlist = []
  24. # max length + 2 spaces
  25. pad = length + 2
  26. ix = 1
  27. for w in mylist:
  28. size = len(w)
  29. # pad with spaces
  30. w = w + ' '*(pad - size)
  31. if ix % 5 == 0:
  32. # add a newline
  33. w = w + '\n'
  34. newlist.append(w)
  35. ix += 1
  36.  
  37. #print newlist
  38.  
  39. # now show the words in the new list
  40. for w in newlist:
  41. print w,
  42.  
  43. """
  44. my display -->
  45. dasj dhsahdwe dhasdhajks ewqhehwq dsajkdhas
  46. edward das dsaw das daswf
  47. fjdk ewf jken dsajkw dskdw
  48. hklt ewq vn1 daskcn daskw
  49. """
I used space padding, because tabs are set differently depending on the editor you want to display with.
Last edited by Ene Uran; Jul 6th, 2009 at 2:05 pm.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,009
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Help with columns formatting

 
0
  #3
Jul 8th, 2009
This might be a little simpler ...
  1. data = """\
  2. dasj dhsahdwe dhasdhajks ewqhehwq dsajkdhas
  3. edward das dsaw das daswf
  4. fjdk ewf jken dsajkw dskdw
  5. hklt ewq vn1 daskcn daskw
  6. """
  7.  
  8. # find the length of the longest word and add 2
  9. length = max(len(w) for w in data.split()) + 2
  10.  
  11. new_data = ""
  12. for line in data.split('\n'):
  13. for word in line.split():
  14. new_data += word.ljust(length)
  15. new_data += '\n'
  16.  
  17. # test it ...
  18. print new_data
Python has functions for string justification.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC