DEV Community

Cover image for Using CSV in Python ⚡
Manitej ⚡
Manitej ⚡

Posted on

Using CSV in Python ⚡

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

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

Alt Text

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

# To read data from CSV file
import csv
# To get random sub-data 
from random import sample
Enter fullscreen mode Exit fullscreen mode

Creating lists to store data

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

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for i in reader:
        print(i)
Enter fullscreen mode Exit fullscreen mode

The above code outputs,

Alt Text

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

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

# stores lists of teams (lists inside inside list)
teams = []
Enter fullscreen mode Exit fullscreen mode

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.

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for i in reader:
        persons.append(i[0])
Enter fullscreen mode Exit fullscreen mode

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

# 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)
Enter fullscreen mode Exit fullscreen mode

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

Full code

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)
Enter fullscreen mode Exit fullscreen mode

Sample output

(for reference only)

Alt Text

Top comments (0)