| | |
Help with columns formatting
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 4
Reputation:
Solved Threads: 0
Hi there,
I have the following string which is tab seperated
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
Thanks a lot.
I have the following string which is tab seperated
Python Syntax (Toggle Plain Text)
dasj dhsahdwe dhasdhajks ewqhehwq dsajkdhas edward das dsaw das daswf fjdk ewf jken dsajkw dskdw 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
Python Syntax (Toggle Plain Text)
dasj dhsahdwe dhasdhajks ewqhehwq dsajkdhas edward das dsaw das daswf fjdk ewf jken dsajkw dskdw hklt ewq vn1 daskcn daskw
Thanks a lot.
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):
I used space padding, because tabs are set differently depending on the editor you want to display with.
python Syntax (Toggle Plain Text)
s = """\ dasj dhsahdwe dhasdhajks ewqhehwq dsajkdhas edward das dsaw das daswf fjdk ewf jken dsajkw dskdw hklt ewq vn1 daskcn daskw """ # create a word list mylist = s.split() #print mylist # find maximum word length length = 0 for w in mylist: size = len(w) if size > length: length = size #print length # pad spaces to the end of each word # add a newline to every fifth word newlist = [] # max length + 2 spaces pad = length + 2 ix = 1 for w in mylist: size = len(w) # pad with spaces w = w + ' '*(pad - size) if ix % 5 == 0: # add a newline w = w + '\n' newlist.append(w) ix += 1 #print newlist # now show the words in the new list for w in newlist: print w, """ my display --> dasj dhsahdwe dhasdhajks ewqhehwq dsajkdhas edward das dsaw das daswf fjdk ewf jken dsajkw dskdw hklt ewq vn1 daskcn daskw """
Last edited by Ene Uran; Jul 6th, 2009 at 2:05 pm.
drink her pretty
This might be a little simpler ...
Python has functions for string justification.
python Syntax (Toggle Plain Text)
data = """\ dasj dhsahdwe dhasdhajks ewqhehwq dsajkdhas edward das dsaw das daswf fjdk ewf jken dsajkw dskdw hklt ewq vn1 daskcn daskw """ # find the length of the longest word and add 2 length = max(len(w) for w in data.split()) + 2 new_data = "" for line in data.split('\n'): for word in line.split(): new_data += word.ljust(length) new_data += '\n' # test it ... print new_data
May 'the Google' be with you!
![]() |
Similar Threads
- Formatting columns from many files (Python)
- The div method for columns doesn't always work (HTML and CSS)
- DataGridView Column Cell Style Formatting (C#)
- Special formatting of a GridView... (ASP.NET)
- printing double array formatting trouble (C)
- formatting problems (Windows NT / 2000 / XP)
- Problems formatting HD (Windows 95 / 98 / Me)
- How to write files to an Excel application (Visual Basic 4 / 5 / 6)
- how to upload Word doc with columns & graphics? (Graphics and Multimedia)
Other Threads in the Python Forum
- Previous Thread: File Handling
- Next Thread: Speed calculating algorithm?
| Thread Tools | Search this Thread |
address aliased anydbm app bash beginner bits calling casino changecolor cipher clear conversion coordinates corners count cturtle curves definedlines development dictionary digital dynamic events examples excel external feet file float format function gui handling hints homework iframe images import input java keycontrol line linux list lists loan loop matching mouse multiple number numbers output parsing path port prime programming projects py py2exe pygame pymailer python random rational raw_input recursion recursive scrolledtext searchingfile shebang signal singleton split string strings tails terminal text threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable web-scrape whileloop word wxpython xlwt






