简体   繁体   中英

Extracting the input records from feature file(bdd format) and convert it into a dictionary/lists/csv/json in python

Scenario: some scenario

Given a set of specific users

| name | department |

| Barry | Beer Cans |

| Pudey | Silly Walks |

| Two-Lumps | Silly Walks |

When we count the number of people in each department

Then we will find two people in "Silly Walks"

But we will find one person in "Beer Cans"

I need to convert the above feature file in python in such a way that I get the below output:

names= ['Barry','Pudey','Two-Lumps']

department =['Beer Cans','Silly Walks','Silly Walks']

dict = {"Name": names, "Department": deartment}

It would be fine if it could be converted to a data frame too.

Can someone please help me?

You can access the data through context.table . In your .py file, in the @given function, add:

names=[]
departments=[]
for row in context.table:
    names.append(row['name'])
    departments.append(row['department'])
#add lists to a dictionary etc.

For a more generic approach, you could do the following instead of the code above:

my_dict={
    heading: [row[heading] for row in context.table] for heading in context.table.headings
}

Pros: dynamic, you can use this code for datasets much larger than the 2 lists in your example Cons: less readable if you're new to Python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM