Using CSV in Python ⚡
Tags: python, tutorial, codenewbie, beginners
Published: 2021-04-05 11:16:12

Hey guys!

I recently had a requirement to make teams from a pool of around 100 people with 5 members per team each. 

Usually, it would take a lot of effort & time to make teams. But in the end, you'll be called biased anyways. That's why I want to automate this process. So I wrote a small script in python which I want to share with you now.

## Requirements

* Generate teams from excel sheet
* Form teams of 5 members from 102 people
* display the left out, 2 people 
* Completely random logic of forming teams

Let's get started!

## Getting CSV Data from excel

I used Google Sheets to collect info, so I need to convert it to CSV.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/04yvyeoy0agc1x7ipzfg.png)

here's the data after conversion (delete the first line, as they indicate column names)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z601nzv4axcscpr3bzfm.png)

I saved the file as `data.csv`

Now comes the main part! The Code :)

## Code 

To read `.CSV` files in Python we need `csv` package. Which is already pre-installed. This means you don't need to install anything to follow along :)

### Importing packages

```python
# To read data from CSV file
import csv
# To get random sub-data 
from random import sample
```

### Creating lists to store data

The `csv` package we imported earlier converts each row in the csv file to a list.

```python
with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for i in reader:
        print(i)
```

The above code outputs,

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a72a7rgunxo1d7a354oe.png)

As you can see it converts each row in CSV file into a python list. Awesome right?

Now I created 2 empty lists to store data

```python
# stores the roll numbers of all members in one list
persons = []

# stores lists of teams (lists inside inside list)
teams = []
```

Now I only want the roll numbers of students to be in `persons` list. To do so we need to access it by using its index.

```python
with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for i in reader:
        persons.append(i[0])
```

Now comes the main part! forming actual teams. 

The idea is to randomly pick numbers from the `persons` lists and make those 5 as a list and insert them into the `teams` list after inserting them into the `teams` list I need to remove those numbers from the `persons` list as there might be a chance a single number enters into 2 teams.

To do so we need `random` package. here's the code

```python
# Repeat the process until there are less than 5 members left
while len(persons)>5:
        # picks 5 random numbers from persons list
        a = sample(persons,5)
        # add the list of numbers as a single team
        teams.append(a)
        # after adding remove them to avoid redundancy
        for i in a:
            persons.remove(i)
```


That's it! now we got our required teams in the `teams` list

## Full code

```python
import csv
from random import sample
persons = []
teams = []
with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for i in reader:
        persons.append(i[0])
    while len(persons)>5:
        a = sample(persons,5)
        teams.append(a)
        for i in a:
            persons.remove(i)
    for i in teams:
        print(i)
    print(persons)
```

## Sample output

(for reference only)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/meiciamlv8wd65dm8ao9.png)