2013-11-05 29 views
1

我想製作一個程序,在極其基本的層次上進行復制,並顯示所有股票價格和它的數量,使它們保持在同一行中,使其看起來很整齊整齊。我試着這樣做代碼:Python:想要在列中均勻地顯示東西

print '{:1s} {:1s} {:1s} {:4.2f} {:1s} {:10d} {:1s}'.format('|','NAME','|',53.63,'|', 10000000, '|') 
print '{:1s} {:1s} {:1s} {:4.2f} {:1s} {:10d} {:1s}'.format('|','NAME','|',4837.34,'|', 1000000000, '|') 

但顯示它們是這樣的:

| NAME | 53.63 | 10000000 | 
| NAME | 4837.34 | 1000000000 | 

,我希望它顯示這樣

| NAME | 53.63 | 10000000 | 
| NAME | 4837.34 | 1000000000 | 

或類似的方式。我希望他們兩邊的線條排成一列,所以看起來更整齊

有沒有辦法讓我能夠實現這個目標?

由於提前

回答

0

假設你有一些製表符分隔的文件中的數據,首先,這應該爲你工作:

def tabularize(infilepath, outfilepath, delim='\t', largeFile=False): 
    """ Return nothing 
     Write into the file in outfilepath, the contents of infilepath, expressed in tabular form. 
     The tabular form is similar to the way in which SQL tables are displayed. 
     If largeFile is set to True, then no caching of lines occurs. However, two passes of the infile are required""" 

    if largeFile: 
     widths = getWidths(infilepath, delim) 
    else: 
     with open(infilepath) as infile: 
      lines = [line.strip().split(delim) for line in infile.readlines() if line.strip()] 
     widths = [max([len(row) for row in rows])+2 for rows in izip_longest(*lines, fillvalue="")] 

     with open(outfilepath, 'w') as outfile: 
      outfile.write("+") 
      for width in widths: 
       outfile.write('-'*width + "+") 
      outfile.write('\n') 
      for line in lines: 
       outfile.write("|") 
       for col,width in izip_longest(line,widths, fillvalue=""): 
        outfile.write("%s%s%s|" %(' '*((width-len(col))/2), col, ' '*((width+1-len(col))/2))) 
       outfile.write('\n+') 
       for width in widths: 
        outfile.write('-'*width + "+") 
       outfile.write('\n') 

def getWidths(infilepath, delim): 
    answer = defaultdict(int) 
    with open(infilepath) as infile: 
     for line in infile: 
      cols = line.strip().split(delim) 
      lens = map(len, cols) 
      for i,l in enumerate(lens): 
       if answer[i] < l: 
        answer[i] = l 

    return [answer[k] for k in sorted(answer)] 

if __name__ == "__main__": 
    print 'starting' 

    infilepath = 'testin' 
    outfilepath = 'testout' 
    tabularize(infilepath, outfilepath, '...', False) 

    print 'done' 
1

事情是這樣的:

print '| {:^4} | {:^7.2f} | {:<10} |'.format('NAME', 53.63, 10000000) 
print '| {:^4} | {:^7.2f} | {:<10} |'.format('NAME',4837.34,1000000000) 

輸出:

| NAME | 53.63 | 10000000 | 
| NAME | 4837.34 | 1000000000 | 

也可以從format通過字段寬度:

print '| {:^{}} | {:^{}.2f} | {:<{}} |'.format('NAME', 4, 53.63, 7, 10000000, 10) 
print '| {:^{}} | {:^{}.2f} | {:<{}} |'.format('NAME', 4, 4837.34, 7, 1000000000, 10) 

輸出:

| NAME | 53.63 | 10000000 | 
| NAME | 4837.34 | 1000000000 | 
1

使用Format Specification Mini-Language描述的文檔中,可以使用一個可選的寬度說明與每個字段(以及其他控制數值的對齊和精度的其他)。

fmt = '| {:^8s} | {:>10,.2f} | {:>14,d} |' 
print fmt.format('NAME', 53.63, 10000000) 
print fmt.format('NAME', 4837.34, 1000000000) 

寬度值也可以混在字段數據:

fmt = '| {:^{}s} | {:>{},.2f} | {:>{},d} |' 
print fmt.format('NAME', 8, 53.63, 10, 10000000, 14) 
print fmt.format('NAME', 8, 4837.34, 10, 1000000000, 14) 

或每個可以在一個單獨的步驟中提供,以保持兩種類型的值開:

fmt = '| {{:^{}s}} | {{:>{},.2f}} | {{:>{},d}} |'.format(8, 10, 14) # widths only 
print fmt.format('NAME', 53.63, 10000000) # data only 
print fmt.format('NAME', 4837.34, 1000000000) 

這是輸出:

| NAME |  53.63 |  10,000,000 | 
| NAME | 4,837.34 | 1,000,000,000 | 

顯然你需要事先知道每列數據的最大寬度。如果不知道,那麼您可能必須檢查每列中的所有值才能找到最大的值,以便確定要使用的正確的字段寬度說明符值 - 假定所有數據在輸出前都可用。