将标题从Cucumber的数据表顶部移动到一侧 - Python



我正在寻找将Cucumber数据表标题更改为侧面的方法。因此,它将使功能文件可读。

普通方式:

| Name | Email   | Phone No. | ......... |
| John | i@g.net | 098765644 | ......... |

它可能是一个非常宽的数据表,我必须来回滚动。

所需的方式:

| Name      | John      |
| Email     | i@g.net   |
| Phone No. | 098765444 |
.
.
.

Java和Ruby中有少数示例。但是我正在与Python合作。

我尝试了许多不同的事情,例如numpy.transpose((,将它们转换为列表。但是它不起作用,因为数据表的格式是:

[<Row['Name','John'],...]

您可以简单地实现此行为,这是我的版本:

def tabledict(table, defaults, aliases = {}):
"""
    Converts a behave context.table to a dictionary.
    Throws NotImplementedError if the table references an unknown key.
    defaults should contain a dictionary with the (surprise) default values.
    aliases makes it possible to map alternative names to the keys of the defaults.
    All keys of the table will be converted to lowercase, you sould make sure that
    you defaults and aliases dictionaries also use lowercase.
    Example:
        Given the book
          | Property                             | Value              |
          | Title                                | The Tragedy of Man |
          | Author                               | Madach, Imre       |
          | International Standard Book Number   | 9631527395         |
    defaults = { "title": "Untitled", "author": "Anonymous", "isbn": None, "publisher": None }
    aliases = { "International Standard Book Number" : "isbn" }
    givenBook = tabledict(context.table, defaults, aliases)
    will give you:
    givenBook == {
        "title": "The Tragedy of Man",
        "author": "Madach, Imre",
        "isbn": 9631527395,
        "publisher": None
        }
"""
initParams = defaults.copy()
validKeys = aliases.keys()[:] + defaults.keys()[:]
for row in table:
    name, value = row[0].lower(), row[1]
    if not name in validKeys:
        raise NotImplementedError(u'%s property is not supported.'%name)
    if name in aliases: name = aliases[name]
    initParams[name] = value
return initParams

这看起来与numpy无关。

旋转列表通常使用zip(*the_list(

完成

这将返回一个旋转的表格

from behave.model import Table

class TurnTable(unittest.TestCase):
    """ 
    """
    def test_transpose(self):
        table = Table(
            ['Name', 'John', 'Mary'],
            rows=[
                ['Email', "john@example.com", "mary@example.com"],
                ['Phone', "0123456789", "9876543210"],
            ])
        aggregate = [table.headings[:]]
        aggregate.extend(table.rows)
        pivoted = list(zip(*aggregate))
        self.assertListEqual(pivoted,
                             [('Name', 'Email', 'Phone'),
                              ('John', 'john@example.com', '0123456789'),
                              ('Mary', 'mary@example.com', '9876543210')])
        pivoted_table = Table(
            pivoted[0],
            rows=pivoted[1:])
        mary = pivoted_table.rows[1]
        self.assertEqual(mary['Name'], 'Mary')
        self.assertEqual(mary['Phone'], '9876543210')

您还可以查看https://pypi.python.org/pypi/pivottable

最新更新