1

I wanted to align all the strings in each column of a file in a particular order using a python script. I have described the problem and the possible outcome using a sample scenario.

#sample.txt

start() "hello"
appended() "fly"
instantiated() "destination"
do() "increment"
logging_sampler() "dummy string"

Output scenario

#sample.txt(indented)

start()           "hello"
appended()        "fly"
instantiated()    "destination"
do()              "increment"
logging_sampler() "dummy string"

So is there any python library that can process a file and provide the above indentation? Is there any general solution such that if I have a file with more that 2 columns and I can still indent all the columns in the same manner?

Kaushik
  • 1,264
  • 8
  • 20
  • 32

3 Answers3

4

So is there any python library that can process a file and provide the above indentation? NO

Is this Possible? Yes

You need to know a way to parse your lines and then display in a formatted manner

In your particular case, parsing is straight forward as you just need to split the string based on the first occurring space. This can easily be done using str.partition. At times you may even need some exotic parsing logic for which you may need to use regex.

Formatting is even simpler, if you know the Format String Syntax.

Demo

>>> for e in st.splitlines():
    left,_,right = e.partition(' ')
    print "{:<20}{:<20}".format(left, right)


start()             "hello"             
appended()          "fly"               
instantiated()      "destination"       
do()                "increment"         
logging_sampler()   "dummy string"  
Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • the above code works for the given scenarios. But is there any general solution such that if I have a file with more that 2 columns and i still need to indent all the columns in the same manner? – Kaushik Sep 30 '13 at 09:49
  • @karthik: Unfortunately, there is no psychic library for this purpose. – Abhijit Sep 30 '13 at 09:52
  • @karthik: You can iterate through the left side items and find out which of them is longest by using [`len()`](http://docs.python.org/2/library/functions.html#len). You can then use [`str.ljust()`](http://docs.python.org/2/library/stdtypes.html#str.ljust) to apply the wanted padding by using the value of the maximum found length and adding some value, say, 1, to it. – miikkas Sep 30 '13 at 10:09
1

Adapted from this one here's a function takes a list of string lists, and returns a list formatted lines.

def table(lines, delim='\t'):
    lens = [len(max(col, key=len)) for col in zip(*lines)]
    fmt = delim.join('{:' + str(x) + '}' for x in lens)
    return [fmt.format(*line) for line in lines]

The rest is trivial:

import re
with open(__file__) as fp:
    lines = [re.split(r' ', s.strip(), maxsplit=1) for s in fp]
print '\n'.join(table(lines))

http://ideone.com/9WucPj

Community
  • 1
  • 1
georg
  • 211,518
  • 52
  • 313
  • 390
0

You can use the tab character ("\t") in your printing, but I'm not clear how you are printing sample.txt.

print string1+"\t"+string2

See here for more details

Community
  • 1
  • 1
philshem
  • 24,761
  • 8
  • 61
  • 127