python 图标题上移_将标题从Cucumber数据表的顶部移到另一侧-Python

本文介绍了一种在Python中将Cucumber DataTable水平转为垂直显示的方法,以提高特征文件的可读性。通过使用`zip(*the_list)`进行转置,并提供了一个具体的例子来展示如何实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am looking for the ways to change the headings of Cucumber's Data Table to the side. So it will make the feature file readable.

Ordinary way:

| Name | Email | Phone No. | ......... |

| John | i@g.net | 098765644 | ......... |

It can be a very wide data table and I would have to scroll back and forth.

Desired way:

| Name | John |

| Email | i@g.net |

| Phone No. | 098765444 |

.

.

.

There are a small number of examples in Java and Ruby. But I am working with Python.

I had tried many different things like numpy.transpose(), converting them to list. But it won't work because the Data Table's format is:

[

解决方案

This doesn't look like it's related to numpy.

pivoting a list of list is often done with zip(*the_list)

This will return a pivoted behave table

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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值