Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-------------------------------------------------------------------------------
- # Name: module1
- # Purpose:
- #
- # Author: Steve
- #
- # Created: 07/12/2011
- # Copyright: (c) Steve 2011
- # Licence: <your licence>
- #-------------------------------------------------------------------------------
- #!/usr/bin/env python
- #Global variables and imported libraries declared here
- import csv
- import os
- import re
- import sys
- data_list = []
- sortedData_list = []
- noOfResults = 100
- column = 6
- message = ("Please press corrisponding key" + '\n\n')
- message = message + (" 1:Search for a keyword" + '\n')
- message = message + (" 2:Search for an exact keyword"+'\n')
- message = message + (" 3:Search for values higher than" + '\n')
- message = message + (" 4:Search for values lower than" + '\n')
- message = message + (" 0:Exit program")
- #-------------------------------------------------------------------------------
- def main():
- # main function just contains function calls
- # ----------------------------------------------------------------------------
- readin()
- setupDirectory()
- setupStylesheet()
- userInput(input(message))
- # ----------------------------------------------------------------------------
- def readin():
- # Read in the csv file and place each line into a seperate line of a list
- # ----------------------------------------------------------------------------
- reader = csv.reader(open('1728520.csv'), delimiter = ',', quotechar = '"')
- for row in reader:
- data_list.append(row)
- del data_list[0]
- #-------------------------------------------------------------------------------
- def setupDirectory():
- # Function to setup directories and paths if they dont exist already
- # ----------------------------------------------------------------------------
- pathdir = os.path.join(os.getcwd(), "output")
- if not os.path.isdir(pathdir):
- os.mkdir(pathdir)
- pathdir = os.path.join(os.getcwd(), "output" , "css" )
- if not os.path.isdir(pathdir):
- os.mkdir(pathdir)
- pathdir = os.path.join(os.getcwd(), "output" , "results" )
- if not os.path.isdir(pathdir):
- os.mkdir(pathdir)
- # ----------------------------------------------------------------------------
- def setupStylesheet():
- # function for setting up the stylesheet used to format data
- # ----------------------------------------------------------------------------
- stylesheet = open((os.path.join(os.getcwd(),"output\\css\\stylesheet.css")), "wt")
- stylesheet.write('body {background-color:#ffffff;}\n')
- stylesheet.write('table {font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width: 100%;border: 1px solid #cef;text-align: left;}\n')
- stylesheet.write('th {font-weight: bold;background-color: #acf;border-bottom: 1px solid #cef; }\n')
- stylesheet.write('td,th {padding: 4px 5px; }\n.odd {background-color: #def; } \n.odd td {border-bottom: 1px solid #cef; }')
- stylesheet.close()
- # ----------------------------------------------------------------------------
- def userInput(a):
- # this function will check to see if the value entered is viable ir not will loop
- # the parameter is the option chosen on the menu
- userOptions = [True,True,True,True,True,True,True,True,True]
- if a == '1':
- searchData(1)
- elif a == '2':
- searchData(2)
- elif a == '3':
- searchData(3)
- elif a == '4':
- searchData(4)
- elif a == '5':
- options(userOptions)
- elif a == '0':
- exit()
- else:
- print('please enter a valid option')
- userInput(input(message))
- def searchData(a):
- # code for searching through the data, the parameter is the choice of option from the user
- # ----------------------------------------------------------------------------
- searchData_list = [] #create a list to store the matched data
- foundInRow = False; #a boolean variable set up for use in the for loops
- if (a == 1):
- searchFor = input("what would you lik to search for?:") #ask the user what to search for
- for row in data_list:
- foundInRow = False
- for cell in row:
- if (re.findall(searchFor ,cell , re.IGNORECASE)):
- foundInRow = True
- break
- if (foundInRow):
- searchData_list.append(row)
- elif (a ==2):
- # the nested for loops sort through each row and then each cell searching for exact data matches
- searchFor = input("what would you lik to search for?:" + '\n\nThis is not case-sensitive but does require the whole field' + '\n(i.e. Agency Staff, would require entry of Agency Staff or AgEnCy sTaFf .etc)') #ask the user what to search for
- for row in data_list:
- foundInRow = False
- for cell in row:
- if (searchFor.lower() == cell.lower()): # to make searching easier made both the cell and the search lower case only to increase matches
- foundInRow = True
- if (foundInRow):
- searchData_list.append(row) #only if there was a match will the data be appended
- elif (a == 3):
- amount = (input ('Enter how minimum expenditure:'))
- for row in data_list:
- foundInRow = False
- if (float(amount) <= Float(re.sub(",","",row[7]))): #takes the sting of the amount
- foundInRow = True
- if (foundInRow):
- searchData_list.append(row)
- elif (a == 4):
- amount = (float(input ('Enter how maximum expenditure:')))
- for row in data_list:
- foundInRow = False
- if (amount >= float(re.sub(",","",data_list[row][7]))): #takes the sting of the amount
- foundInRow = True
- if (foundInRow):
- searchData_list.append(row)
- #This code tells the user if no data was found on their search and will ask if they want to try again
- if (len(searchData_list) == 0):
- answer = ' '
- while(answer.lower() != 'y' and answer.lower() != 'n'):
- answer = input("No data found would you like to try again? (Y/N)")
- if (answer == 'n'):
- exit()
- if (answer == 'y'):
- userInput(input(message))
- print (searchData_list)
- createOutputFile(searchData_list)
- # ----------------------------------------------------------------------------
- def createOutputFile(searchData_list):
- # write the data as a HTML file
- # ----------------------------------------------------------------------------
- sortedData_list = sorted(data_list,key=lambda x:x[column])
- output_file = open((os.path.join(os.getcwd(),"output\\results\\test.html")), "wt")
- output_file.write('<html> \n' + '<head> \n')
- output_file.write('<link rel="stylesheet" type="text/css" ')
- output_file.write('href="'+(os.path.join(os.getcwd(),"output\\css\\stylesheet.css")) +'" /> \n')
- output_file.write('</head> \n' +'<body>\n ' + '<table border="1">\n')
- #print(sortedData_list)
- firstRow = 0
- for row in searchData_list:
- # for in range(1-100):
- output_file.write('<tr>')
- ## if row == 0:
- ## for cell in row:
- ## output_file.write('<th>' + cell + '</th>')
- ## else:
- for cell in row:
- output_file.write('<td>' + cell + '</td>')
- output_file.write('</tr>')
- output_file.write('</table id="data">' + '</body>' + '</html>')
- output_file.close()
- # ----------------------------------------------------------------------------
- def options(userOptions):
- optionMessage = 'Enter the number to toggle options: \n'
- optionMessage = optionMessage + ' 1:Show "Department Family" in output\n'
- optionMessage = optionMessage + ' Currently:' + boolToStr(userOptions[0]) + '\n'
- optionMessage = optionMessage + ' 2:Show "Entity" in output\n'
- optionMessage = optionMessage + ' Currently:' + boolToStr(userOptions[1]) + '\n'
- optionMessage = optionMessage + ' 3:Show "Date Of Payment" in output\n'
- optionMessage = optionMessage + ' Currently:' + boolToStr(userOptions[2]) + '\n'
- optionMessage = optionMessage + ' 4:Show "Expense Type" in output\n'
- optionMessage = optionMessage + ' Currently:' + boolToStr(userOptions[3]) + '\n'
- optionMessage = optionMessage + ' 5:Show "Expense Area" in ouput\n'
- optionMessage = optionMessage + ' Currently:' + boolToStr(userOptions[4]) + '\n'
- optionMessage = optionMessage + ' 6:Show "Supplier" in ouput\n'
- optionMessage = optionMessage + ' Currently:' + boolToStr(userOptions[5]) + '\n'
- optionMessage = optionMessage + ' 7:Show "Transaction Number" in ouput\n'
- optionMessage = optionMessage + ' Currently:' + boolToStr(userOptions[6]) + '\n'
- optionMessage = optionMessage + ' 8:Show "Amount In Sterlin" in ouput\n'
- optionMessage = optionMessage + ' Currently:' + boolToStr(userOptions[7]) + '\n'
- optionMessage = optionMessage + ' 9:Change the amount of rows per output file\n'
- optionMessage = optionMessage + ' Currently:' + boolToStr(userOptions[8]) + '\n'
- optionMessage = optionMessage + ' 0:Exit to main manu'
- option = (int(input(optionMessage)))
- if (option > 0 and option < 9):
- if userOptions[option-1] == True:
- userOptions[option-1] = False
- else:
- userOptions[option-1] = True
- elif (option == 0):
- userInput(input(message))
- options(userOptions)
- def boolToStr(boolean):
- if boolean == True:
- return 'True'
- else:
- return 'False'
- def toFloat(stringy):
- print(stringy)
- def exit():
- sys.exit(0)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment