def buckets(filename, bucketName, separator, classColumn):
 numberOfBuckets = 10
 data = {}
 with open(filename) as f:
  lines = f.readlines()
 for line in lines:
  if (separator != ' DCTB '):
   line = line.replace(separator, ' DCTB ')
  category = line.split()[classColumn]
  data.setdefault(category, [])
  data[category].append(line)
 buckets = []
 for i in range(numberOfBuckets):
  buckets.append([])
 for k in data.keys():
  random.shuffle(data[k])
  bNum = 0
  for item in data[k]:
   buckets[bNum].append(item)
   bNum = ((bNum + 1) % numberOfBuckets)
 for bNum in range(numberOfBuckets):
  f = open(('%s-%02i' % (bucketName, (bNum + 1))), 'w')
  for item in buckets[bNum]:
   f.write(item)
  f.close()

def buckets ( filename , bucketName , separator , classColumn ) : numberOfBuckets = 10 data = { } with open ( filename ) as f : lines = f . readlines ( ) for line in lines : if ( separator != ' DCTB ' ) : line = line . replace ( separator , ' DCTB ' ) category = line . split ( ) [ classColumn ] data . setdefault ( category , [ ] ) data [ category ] . append ( line ) buckets = [ ] for i in range ( numberOfBuckets ) : buckets . append ( [ ] ) for k in data . keys ( ) : random . shuffle ( data [ k ] ) bNum = 0 for item in data [ k ] : buckets [ bNum ] . append ( item ) bNum = ( ( bNum + 1 ) % numberOfBuckets ) for bNum in range ( numberOfBuckets ) : f = open ( ( '%s-%02i' % ( bucketName , ( bNum + 1 ) ) ) , 'w' ) for item in buckets [ bNum ] : f . write ( item ) f . close ( )
the original data is in the file named filename bucketname is the prefix for all the bucket names separator is the character that divides the columns (for ex .