【问题标题】:Python PrettyTable printing table on same linePython PrettyTable 在同一行打印表格
【发布时间】:2014-09-08 10:16:14
【问题描述】:

我正在使用 PrettyTable 'from_db_cursor' 模块来很好地打印我从我的 sql 请求中得到的响应。一切正常,我唯一的问题是,对于某些查询,我想打印它们附加到同一行的表,如下所示:

countUnivers1:    countUnivers2:    countUnivers3:
+----------+      +----------+      +----------+
| COUNT(*) |      | COUNT(*) |      | COUNT(*) |
+----------+      +----------+      +----------+
| 1681943  |      |  51954   |      | 4140984  |
+----------+      +----------+      +----------+

但我无法找到如何做到这一点,这是我目前使用的代码:

i = 0
tables = []
with open(output, 'w+') as file:
    file.write(str(current_time) + '\n')
    for query in QUERIES.items():
        cur.execute(query[1])
        table = from_db_cursor(cur)
        if not re.search('countUnivers' ,query[0]):
            file.write('\n' + query[0] + ':\n')
            file.write(str(table) + '\n')
        else:
            if i < 6:
                file.write(query[0] + ':\t')
                tables.append(str(table))
                i += 1
            elif i == 6:
                file.write('\n')
                for t in tables:
                    file.write(str(table) + '\t')
                i = 0
                tables = []
    file.write('\nDatabase:\n' + json.dumps(dbParams, indent=4) + '\n')

这段代码输出我:

countUnivers1:  countUnivers2:  countUnivers3:
+----------+
| COUNT(*) |
+----------+
| 1681943  |
+----------+    +----------+
| COUNT(*) |
+----------+
|  51954   |
+----------+    +----------+
| COUNT(*) |
+----------+
| 4140984  |
+----------+

这里的QUERIES是一个OrderedDict,里面装满了SQL请求:

('countUnivers1', "SELECT COUNT(*) \
                        FROM ourson_offer o \
                        WHERE o.cat LIKE '1%' \
                        AND CHARACTER_LENGTH(o.cat) = 7"),

喜欢这个。

【问题讨论】:

    标签: python python-2.7 mysql-python prettytable


    【解决方案1】:

    文本是二维的:行+行中的字符。 “表”是一个包含新行的 Python 字符串——它是一个文本!在每个行尾字符(CRLF、CR 或 LF 取决于操作系统/平台)之后开始一个新行。

    您想做的事情需要了解字符串的结构作为文本并垂直对齐新块。

    def pad_lines_vertically(lines, size):
        ''' List of lines of exactly `size` length.
        Extended with empty lines if needed.
        '''
        orig_lines = list(lines)
        assert size >= len(orig_lines)
        return orig_lines + [''] * (size - len(orig_lines))
    
    def pad_lines_horizontally(lines):
        ''' Pad lines to the lenght of the longest line.
        '''
        line_length = max(len(line) for line in lines)
        return [
            line.ljust(line_length)
            for line in lines
        ]
    
    def text_add(text1, text2, padding=' '):
        lines1 = text1.splitlines()
        lines2 = text2.splitlines()
        line_count = max(len(lines1), len(lines2))
    
        def pad_lines(lines):
            return pad_lines_horizontally(
                pad_lines_vertically(lines, line_count)
            )
    
        return '\n'.join(
            ''.join(line1 + padding + line2)
            for line1, line2 in zip(pad_lines(lines1), pad_lines(lines2))
        )
    

    这样使用:

    text1 = '''\
    countUnivers1:
    +----------+
    | COUNT(*) |
    +-------
    | 1681943  |
    +----------+
    '''
    
    text2 = '''\
    countUnivers2:
    +----------+
    | COUNT(*) |
    +----------+
    |  51954   |
    +----------+
     plus a
     comment
    '''
    
    print text_add(text1, text2, padding='\t')
    
    
    countUnivers1:  countUnivers2:
    +----------+    +----------+
    | COUNT(*) |    | COUNT(*) |
    +-------        +----------+
    | 1681943  |    |  51954   |
    +----------+    +----------+
                     plus a
                     comment
    

    【讨论】:

      猜你喜欢
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 2021-09-09
      • 1970-01-01
      相关资源
      最近更新 更多