Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 2.59 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python2
  2. #-*- coding: utf-8 -*-
  3.  
  4.  
  5. """
  6.     Module for transforming group lists to Moodle format
  7.  
  8.     Lists must be in CSV format "surname name,id".
  9.     Output data is CSV "firstname;username;password;lastname;email;idnumber.
  10. """
  11.  
  12.  
  13. import csv
  14. import sys
  15. import trans
  16.  
  17.  
  18. class ProcessCSV():
  19.     def __init__(self, input_filename, output_filename):
  20.         self.input_filename = input_filename
  21.         self.output_filename = output_filename
  22.  
  23.     def construct(self):
  24.         try:
  25.             open(self.input_filename)
  26.         except IOError:
  27.             print(":: Input file does not exist")
  28.             return
  29.  
  30.         input_file = open(self.input_filename)
  31.         reader = csv.reader(input_file)
  32.  
  33.         output_file = open(self.output_filename, "w")
  34.         writer = csv.writer(output_file, delimiter=";")
  35.  
  36.         is_first_row = True
  37.         for row in reader:
  38.             if is_first_row:
  39.                 # Construct new header
  40.                 row[0] = "firstname"
  41.                 row[1] = "username"
  42.                 row.append("password")
  43.                 row.append("lastname")
  44.                 row.append("email")
  45.                 row.append("idnumber")
  46.  
  47.                 is_first_row = False
  48.             else:
  49.                 lastname = row[0].split()[0]
  50.                 firstname = row[0][len(lastname) + 1:]
  51.                 personal_id = row[1]
  52.  
  53.                 # Firstname
  54.                 row[0] = firstname
  55.  
  56.                 # Username (same as ID)
  57.                 # 7 is ID length (we need to save zeros in the beginning)
  58.                 row[1] = "{0:07d}".format(int(personal_id))
  59.  
  60.                 # Password (surname translated in lowercase)
  61.                 row.append(self.translate(lastname).lower())
  62.  
  63.                 # Lastname
  64.                 row.append(lastname)
  65.  
  66.                 # Email (filled by default value)
  67.                 row.append("test@test.com")
  68.  
  69.                 # ID
  70.                 # 7 is ID length (we need to save zeros in the beginning)
  71.                 row.append("{0:07d}".format(int(personal_id)))
  72.  
  73.             writer.writerow(row)
  74.  
  75.         input_file.close()
  76.         output_file.close()
  77.  
  78.     def translate(self, string):
  79.         return self.to_utf8(self.to_unicode(string).encode("trans"))
  80.  
  81.     def to_utf8(self, string):
  82.         return string.encode("utf-8")
  83.  
  84.     def to_unicode(self, string):
  85.         return unicode(string, "utf-8")
  86.  
  87.  
  88. def main(arguments):
  89.     app = ProcessCSV(arguments[0], arguments[1])
  90.     app.construct()
  91.  
  92.  
  93. if __name__ == "__main__":
  94.     if (len(sys.argv) != 3):
  95.         print(":: Usage: script.py input.csv output.csv")
  96.     else:
  97.         # Ignore self-name
  98.         main(sys.argv[1:])