Advertisement
dougllio

NFLDraft.py

May 9th, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.63 KB | None | 0 0
  1. ######################
  2. ## Reads in NFL Draft Data, Sorts, then Writes to a file
  3. ## Data file using data copied/pasted from Wikipedia
  4. ## http://en.wikipedia.org/wiki/2014_NFL_Draft
  5. ## http://drafthistory.com/
  6. ######################
  7.  
  8. # Column Headers
  9. header = ("Round","Pick","NFL Team","Player","Position","College","Conference")
  10.  
  11. # draftList will contain a matrix (list of lists) Each outer list will represent
  12. # a row read from the file, and each inner list a column from that row
  13. draftList = []
  14.  
  15. #Open the file and read/split
  16. with open("NFLDraft.txt","r") as draftFile:
  17.  
  18.   # Read entire text file into a temporary list with each row as a new member of the list
  19.   draftRows = draftFile.read().splitlines()
  20.  
  21.   # Loop through each row and split it into a list
  22.   for row in draftRows:
  23.  
  24.     # The data was copied from a table in Wikipedia and separates each column with tabs
  25.     rowList = row.split("\t")
  26.  
  27.     # Verify that the data is what we expect
  28.     if len(rowList)>6 and rowList[0].isdigit() and rowList[1].isdigit():
  29.  
  30.       # First two columns are numbers so we need to convert them
  31.       # from strings to integers in order for sorting to work
  32.       rowList[0] = int(rowList[0])
  33.       rowList[1] = int(rowList[1])
  34.  
  35.       # Append the list row to our list of lists
  36.       draftList.append(rowList)
  37.  
  38.     else:
  39.  
  40.       print "(!) Invalid row read. Data possibly corrupt?: {}".format(row)
  41.  
  42. # If no valid rows read, bail out
  43. if len(draftList)==0:
  44.   print "No valid data read"
  45.   quit()
  46.  
  47. # Start the output
  48. print
  49. print "NFL Draft Results Sorter"
  50. print
  51.  
  52. # Set up variables we'll use in the while loop
  53. choice = -1
  54. oldChoice = -1
  55. reverseSort = False
  56.  
  57. # We will display the data and sort until they enter "9" to save and exit
  58. while choice <> 9:
  59.  
  60.     # Column header names and numbers. Numbers are what the user will sort by
  61.     # Formatting ensures that the output is aligned.
  62.     # http://stackoverflow.com/questions/13451989/pythons-many-ways-of-string-formatting-are-the-older-ones-going-to-be-deprec?lq=1
  63.     headerText = " {:5} {:4} {:25} {:25} {:8} {:20} {:10} ".format(header[0],header[1],header[2],header[3],header[4],header[5],header[6])
  64.     headerTextNum = " {:5} {:4} {:<25} {:<25} {:<8} {:<20} {:<10} ".format(0,1,2,3,4,5,6)
  65.  
  66.     # Print headers
  67.     print headerText
  68.     print headerTextNum
  69.     print "="*len(headerText)
  70.  
  71.     # Loop through each row and print it out
  72.     # Formatting ensures that the output is aligned.
  73.     # http://stackoverflow.com/questions/13451989/pythons-many-ways-of-string-formatting-are-the-older-ones-going-to-be-deprec?lq=1
  74.     for row in draftList:
  75.           print " {:5} {:4} {:25} {:25} {:8} {:20} {:10} ".format(row[0],row[1],row[2],row[3],row[4],row[5],row[6])
  76.     print
  77.  
  78.     # Ask which column to sort by
  79.     print "Which column do you want to sort by? (Enter column header number or 9 to save and exit)"
  80.     strChoice = raw_input(": ")
  81.  
  82.     # Only proceed if they enter a number
  83.     if strChoice.isdigit():
  84.      
  85.         # Store a copy of the previous choice
  86.         oldChoice = choice
  87.  
  88.         # Convert the raw input to an int
  89.         choice = int(strChoice)
  90.  
  91.         # Figure out if they are trying to sort the same column in reverse
  92.         if choice==oldChoice:
  93.             reverseSort = (not reverseSort)
  94.         else:
  95.             reverseSort = False
  96.     else:
  97.         # If the menu choice is bad data, reset the choice variable so it won't accidentally resort
  98.         choice = -1
  99.  
  100.     # If they picked a proper column, sort it by the column selected. Since we
  101.     # have a list of lists, we need to tell the sort function how to sort. In
  102.     # this case we sort by the value of the column selected. We sort the list of
  103.     # lists (the outer list) by using a specific value in each inner list.
  104.     # Kind of like sorting a list of words using the third letter in each word.
  105.     # http://stackoverflow.com/questions/3121979/how-to-sort-list-tuple-of-lists-tuples/3121985#3121985
  106.     if choice >=0 and choice < len(header):
  107.         draftList.sort(key=lambda x:x[choice], reverse=reverseSort)
  108.  
  109. # When they enter "9" the loop ends and we save the new list of lists in a new file
  110. with open("NFLDraftSorted.txt","w") as draftFile:
  111.  
  112.   # Loop through each row
  113.   for row in draftList:
  114.  
  115.     # We split() the data when we read the file, now we need to join() when we save
  116.     # We use the generator to join because join() only works on strings.
  117.     # http://stackoverflow.com/questions/3590165/joining-list-has-integer-values-with-python
  118.     rowOut = ",".join(str(x) for x in row)
  119.  
  120.     # Write the row to the file along with a CR/LF character
  121.     draftFile.write(rowOut+"\r\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement