Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have imported a file containing 8 columns, each column separated by ' ', so during import all ' ' are replaced by 'space' and ' ' are split to make indexing easier for me. Then a list is generated form input file for further analysis. But after analysis I got

lst =  ['PAIR', '1MFK', 'URANIUM', '82', 'HELIUM', '112', 3.6997']

kind of list, so I put

final = '    '.join(lst)   

to get the values like PAIR 1MFK 1 URANIUM 82 HELIUM 112 3.6997
But when I print all values it comes as following

PAIR 1MFK 1 URANIUM 82 HELIUM 112 3.6997  
PAIR 2JGH 2 PLUTONIUM 98 POTASSIUM 88 5.3003  
PAIR 345G 3 SODIUM 23 CARBON 14 1.664  
PAIR 4IG5 4 LITHIUM 82 ARGON 99 2.5506 

here the column entries don't lie just one below another however indentation is same for all.

I have tried with ' ' but that produce same randomized result.
please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
171 views
Welcome To Ask or Share your Answers For Others

1 Answer

If I understand you correctly, you can do something simple like this..

>>> s = '''PAIR 1MFK 1 URANIUM 82 HELIUM 112 3.6997  
... PAIR 2JGH 2 PLUTONIUM 98 POTASSIUM 88 5.3003  
... PAIR 345G 3 SODIUM 23 CARBON 14 1.664  
... PAIR 4IG5 4 LITHIUM 82 ARGON 99 2.5506'''
>>> ll = [x.split() for x in s.split('
')]
>>> for row in ll:
...   print ''.join(x.ljust(10) for x in row)
... 
PAIR      1MFK      1         URANIUM   82        HELIUM    112       3.6997    
PAIR      2JGH      2         PLUTONIUM 98        POTASSIUM 88        5.3003    
PAIR      345G      3         SODIUM    23        CARBON    14        1.664     
PAIR      4IG5      4         LITHIUM   82        ARGON     99        2.5506    

Here I am using a fixed column width of 10 characters. If you like, you can make it slightly more sophisticated by calculating appropriate column widths beforehand.

>>> column_widths = [max(len(x) for x in l) for l in zip(*ll)]
>>> for row in ll:
...   print '  '.join(x.ljust(w) for x,w in zip(row, column_widths))
... 
PAIR  1MFK  1  URANIUM    82  HELIUM     112  3.6997
PAIR  2JGH  2  PLUTONIUM  98  POTASSIUM  88   5.3003
PAIR  345G  3  SODIUM     23  CARBON     14   1.664 
PAIR  4IG5  4  LITHIUM    82  ARGON      99   2.5506

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...