Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ######################
- ## Reads in NFL Draft Data, Sorts, then Writes to a file
- ## Data file using data copied/pasted from Wikipedia
- ## http://en.wikipedia.org/wiki/2014_NFL_Draft
- ## http://drafthistory.com/
- ######################
- # Column Headers
- header = ("Round","Pick","NFL Team","Player","Position","College","Conference")
- # draftList will contain a matrix (list of lists) Each outer list will represent
- # a row read from the file, and each inner list a column from that row
- draftList = []
- #Open the file and read/split
- with open("NFLDraft.txt","r") as draftFile:
- # Read entire text file into a temporary list with each row as a new member of the list
- draftRows = draftFile.read().splitlines()
- # Loop through each row and split it into a list
- for row in draftRows:
- # The data was copied from a table in Wikipedia and separates each column with tabs
- rowList = row.split("\t")
- # Verify that the data is what we expect
- if len(rowList)>6 and rowList[0].isdigit() and rowList[1].isdigit():
- # First two columns are numbers so we need to convert them
- # from strings to integers in order for sorting to work
- rowList[0] = int(rowList[0])
- rowList[1] = int(rowList[1])
- # Append the list row to our list of lists
- draftList.append(rowList)
- else:
- print "(!) Invalid row read. Data possibly corrupt?: {}".format(row)
- # If no valid rows read, bail out
- if len(draftList)==0:
- print "No valid data read"
- quit()
- # Start the output
- print
- print "NFL Draft Results Sorter"
- print
- # Set up variables we'll use in the while loop
- choice = -1
- oldChoice = -1
- reverseSort = False
- # We will display the data and sort until they enter "9" to save and exit
- while choice <> 9:
- # Column header names and numbers. Numbers are what the user will sort by
- # Formatting ensures that the output is aligned.
- # http://stackoverflow.com/questions/13451989/pythons-many-ways-of-string-formatting-are-the-older-ones-going-to-be-deprec?lq=1
- headerText = " {:5} {:4} {:25} {:25} {:8} {:20} {:10} ".format(header[0],header[1],header[2],header[3],header[4],header[5],header[6])
- headerTextNum = " {:5} {:4} {:<25} {:<25} {:<8} {:<20} {:<10} ".format(0,1,2,3,4,5,6)
- # Print headers
- print headerText
- print headerTextNum
- print "="*len(headerText)
- # Loop through each row and print it out
- # Formatting ensures that the output is aligned.
- # http://stackoverflow.com/questions/13451989/pythons-many-ways-of-string-formatting-are-the-older-ones-going-to-be-deprec?lq=1
- for row in draftList:
- print " {:5} {:4} {:25} {:25} {:8} {:20} {:10} ".format(row[0],row[1],row[2],row[3],row[4],row[5],row[6])
- print
- # Ask which column to sort by
- print "Which column do you want to sort by? (Enter column header number or 9 to save and exit)"
- strChoice = raw_input(": ")
- # Only proceed if they enter a number
- if strChoice.isdigit():
- # Store a copy of the previous choice
- oldChoice = choice
- # Convert the raw input to an int
- choice = int(strChoice)
- # Figure out if they are trying to sort the same column in reverse
- if choice==oldChoice:
- reverseSort = (not reverseSort)
- else:
- reverseSort = False
- else:
- # If the menu choice is bad data, reset the choice variable so it won't accidentally resort
- choice = -1
- # If they picked a proper column, sort it by the column selected. Since we
- # have a list of lists, we need to tell the sort function how to sort. In
- # this case we sort by the value of the column selected. We sort the list of
- # lists (the outer list) by using a specific value in each inner list.
- # Kind of like sorting a list of words using the third letter in each word.
- # http://stackoverflow.com/questions/3121979/how-to-sort-list-tuple-of-lists-tuples/3121985#3121985
- if choice >=0 and choice < len(header):
- draftList.sort(key=lambda x:x[choice], reverse=reverseSort)
- # When they enter "9" the loop ends and we save the new list of lists in a new file
- with open("NFLDraftSorted.txt","w") as draftFile:
- # Loop through each row
- for row in draftList:
- # We split() the data when we read the file, now we need to join() when we save
- # We use the generator to join because join() only works on strings.
- # http://stackoverflow.com/questions/3590165/joining-list-has-integer-values-with-python
- rowOut = ",".join(str(x) for x in row)
- # Write the row to the file along with a CR/LF character
- draftFile.write(rowOut+"\r\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement