2012-07-05 15 views
7

पर पाइथन डेटा संरचना को आउटपुट करने के कुछ दृष्टिकोण क्या हैं मेरे पास पाइथन में टुपल्स की एक सूची है जिसे मैं पुन: संरचित पाठ में एक तालिका में आउटपुट करना चाहता हूं।पुन: संरचित पाठ

डॉक्यूटिल्स लाइब्रेरी को अन्य स्वरूपों में पुनर्वित्तित पाठ को परिवर्तित करने के लिए बहुत अच्छा समर्थन है, लेकिन मैं स्मृति में डेटा संरचना से सीधे पुनर्गठित पाठ में लिखना चाहता हूं।

उत्तर

5

मुझे पाइथन डेटा संरचनाओं से आरएसटी आउटपुट करने के लिए किसी भी पुस्तकालय से अवगत नहीं है, लेकिन इसे स्वयं प्रारूपित करना बहुत आसान है।

>>> data = [('hey', 'stuff', '3'), 
      ('table', 'row', 'something'), 
      ('xy', 'z', 'abc')] 
>>> numcolumns = len(data[0]) 
>>> colsizes = [max(len(r[i]) for r in data) for i in range(numcolumns)] 
>>> formatter = ' '.join('{:<%d}' % c for c in colsizes) 
>>> rowsformatted = [formatter.format(*row) for row in data] 
>>> header = formatter.format(*['=' * c for c in colsizes]) 
>>> output = header + '\n' + '\n'.join(rowsformatted) + '\n' + header 
>>> print output 
===== ===== ========= 
hey stuff 3   
table row something 
xy z  abc  
===== ===== ========= 
6
>> print make_table([['Name', 'Favorite Food', 'Favorite Subject'], 
        ['Joe', 'Hamburgers', 'Cars'], 
        ['Jill', 'Salads', 'American Idol'], 
        ['Sally', 'Tofu', 'Math']]) 

+------------------+------------------+------------------+ 
| Name    | Favorite Food | Favorite Subject | 
+==================+==================+==================+ 
| Joe    | Hamburgers  | Cars    | 
+------------------+------------------+------------------+ 
| Jill    | Salads   | American Idol | 
+------------------+------------------+------------------+ 
| Sally   | Tofu    | Math    | 
+------------------+------------------+------------------+ 

यहाँ कोड मैं त्वरित और गंदी reStructuredText तालिकाओं के लिए उपयोग किया जाता है::

def make_table(grid): 
    cell_width = 2 + max(reduce(lambda x,y: x+y, [[len(item) for item in row] for row in grid], [])) 
    num_cols = len(grid[0]) 
    rst = table_div(num_cols, cell_width, 0) 
    header_flag = 1 
    for row in grid: 
     rst = rst + '| ' + '| '.join([normalize_cell(x, cell_width-1) for x in row]) + '|\n' 
     rst = rst + table_div(num_cols, cell_width, header_flag) 
     header_flag = 0 
    return rst 

def table_div(num_cols, col_width, header_flag): 
    if header_flag == 1: 
     return num_cols*('+' + (col_width)*'=') + '+\n' 
    else: 
     return num_cols*('+' + (col_width)*'-') + '+\n' 

def normalize_cell(string, length): 
    return string + ((length - len(string)) * ' ') 
2

@ cieplak के जवाब बढ़िया था यहाँ एक आरएसटी मेज पर अजगर tuples की सूची प्रारूपित करने का एक उदाहरण है । मैं यह थोड़ा परिष्कृत ताकि कॉलम आकार के होते हैं स्वतंत्र रूप से

print make_table([  ['Name', 'Favorite Food', 'Favorite Subject'], 
          ['Joe', 'Hamburgrs', 'I like things with really long names'], 
          ['Jill', 'Salads', 'American Idol'], 
          ['Sally', 'Tofu', 'Math']]) 

    ===== ============= ==================================== 
    Name Favorite Food Favorite Subject      
    ===== ============= ==================================== 
    Joe Hamburgrs  I like things with really long names 
    ----- ------------- ------------------------------------ 
    Jill Salads  American Idol       
    ----- ------------- ------------------------------------ 
    Sally Tofu   Math         
    ===== ============= ==================================== 

यहाँ कोड

def make_table(grid): 
    max_cols = [max(out) for out in map(list, zip(*[[len(item) for item in row] for row in grid]))] 
    rst = table_div(max_cols, 1) 

    for i, row in enumerate(grid): 
     header_flag = False 
     if i == 0 or i == len(grid)-1: header_flag = True 
     rst += normalize_row(row,max_cols) 
     rst += table_div(max_cols, header_flag) 
    return rst 

def table_div(max_cols, header_flag=1): 
    out = "" 
    if header_flag == 1: 
     style = "=" 
    else: 
     style = "-" 

    for max_col in max_cols: 
     out += max_col * style + " " 

    out += "\n" 
    return out 


def normalize_row(row, max_cols): 
    r = "" 
    for i, max_col in enumerate(max_cols): 
     r += row[i] + (max_col - len(row[i]) + 1) * " " 

    return r + "\n" 
0

यहाँ @ cieplak के कोड स्ट्रिंग और बहु ​​समर्थन करने के लिए रूपांतरण जोड़ने है। शायद यह किसी के लिए उपयोग किया जाएगा।

def make_table(grid): 
cell_width = 2 + max(reduce(lambda x,y: x+y, [[max(map(len, str(item).split('\n'))) for item in row] for row in grid], [])) 
num_cols = len(grid[0]) 
rst = table_div(num_cols, cell_width, 0) 
header_flag = 1 
for row in grid: 
    split_row = [str(cell).split('\n') for cell in row] 
    lines_remaining = 1 

    while lines_remaining>0: 
     normalized_cells = [] 
     lines_remaining = 0 
     for cell in split_row: 
      lines_remaining += len(cell) 

      if len(cell) > 0: 
       normalized_cell = normalize_cell(str(cell.pop(0)), cell_width - 1) 
      else: 
       normalized_cell = normalize_cell('', cell_width - 1) 

      normalized_cells.append(normalized_cell) 

     rst = rst + '| ' + '| '.join(normalized_cells) + '|\n' 

    rst = rst + table_div(num_cols, cell_width, header_flag) 
    header_flag = 0 
return rst 
6

tabulate पैकेज देखें। यह कर सकते हैं उत्पादन आरएसटी प्रारूप द्वारा:

print tabulate(table, headers, tablefmt="rst") 
0

आप अजगर से एक CSV में डंप करने के लिए चुन सकते हैं और उसके बाद के रूप में http://docutils.sourceforge.net/docs/ref/rst/directives.html#csv-table में यह एक मिला है आरएसटी का csv-टेबल सुविधा का उपयोग: फ़ाइल: निर्देश बस शामिल करने के लिए डेटा के साथ एक सीएसवी फ़ाइल।

संबंधित मुद्दे