Sclafus

randomizer and csv reader

May 15th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. import random
  2. def csv_reader(filename):
  3.     ''' gives back a list of every element in the csv file  '''
  4.    
  5.     a = []
  6.     try:
  7.         with open(filename, "r") as csvfile:
  8.             for row in csvfile:
  9.                 tmp = row.split(";")
  10.                 a.append(tmp[0])
  11.     except FileNotFoundError:
  12.         print("Could not find file, please retry.")
  13.         main()
  14.     return a
  15.  
  16. def randomizer(a:list, y:int) -> (list):
  17.     '''it randomizes the elements in a list into different y different teams'''
  18.     b = [[]]
  19.     for num in range(y):
  20.         b.append([])
  21.     for element in a:
  22.         tmp = (random.randrange(0, y+1))
  23.         b[tmp].append(element)
  24.     return b
  25.  
  26.  
  27.  
  28. def main():
  29.     filename = input("Inserire nome file o path: ")
  30.     if filename[-4:] != '.csv':
  31.         filename = filename + '.csv'
  32.     a = csv_reader(filename)
  33.     y = int(input("Inserire il numero di team: "))
  34.     b = randomizer(a, y)
  35.     print(b)
  36.    
  37. main()
Advertisement
Add Comment
Please, Sign In to add comment