Guest User

Untitled

a guest
Apr 26th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.91 KB | None | 0 0
  1. #-------------------------------------------------------------------------------
  2. # Name:        module1
  3. # Purpose:
  4. #
  5. # Author:      Steve
  6. #
  7. # Created:     07/12/2011
  8. # Copyright:   (c) Steve 2011
  9. # Licence:     <your licence>
  10. #-------------------------------------------------------------------------------
  11. #!/usr/bin/env python
  12.  
  13.  
  14. #Global variables and imported libraries declared here
  15. import csv
  16. import os
  17. import re
  18. import sys
  19. data_list = []
  20. sortedData_list = []
  21. noOfResults = 100
  22. column = 6
  23. message = ("Please press corrisponding key" + '\n\n')
  24. message = message + ("    1:Search for a keyword" + '\n')
  25. message = message + ("    2:Search for an exact keyword"+'\n')
  26. message = message + ("    3:Search for values higher than" + '\n')
  27. message = message + ("    4:Search for values lower than" + '\n')
  28. message = message + ("    0:Exit program")
  29. #-------------------------------------------------------------------------------
  30.  
  31.  
  32.  
  33. def main():
  34. #   main function just contains function calls
  35. #   ----------------------------------------------------------------------------
  36.  
  37.     readin()
  38.     setupDirectory()
  39.     setupStylesheet()
  40.     userInput(input(message))
  41. #   ----------------------------------------------------------------------------
  42.  
  43.  
  44. def readin():
  45. #   Read in the csv file and place each line into a seperate line of a list
  46. #   ----------------------------------------------------------------------------
  47.     reader = csv.reader(open('1728520.csv'), delimiter = ',', quotechar = '"')
  48.  
  49.     for row in reader:
  50.         data_list.append(row)
  51.     del data_list[0]
  52.  
  53. #-------------------------------------------------------------------------------
  54.  
  55.  
  56. def setupDirectory():
  57. #   Function to setup directories and paths if they dont exist already
  58. #   ----------------------------------------------------------------------------
  59.    pathdir = os.path.join(os.getcwd(), "output")
  60.    if not os.path.isdir(pathdir):
  61.     os.mkdir(pathdir)
  62.  
  63.    pathdir = os.path.join(os.getcwd(), "output" , "css" )
  64.    if not os.path.isdir(pathdir):
  65.     os.mkdir(pathdir)
  66.  
  67.    pathdir = os.path.join(os.getcwd(), "output" , "results" )
  68.    if not os.path.isdir(pathdir):
  69.     os.mkdir(pathdir)
  70. #   ----------------------------------------------------------------------------
  71.  
  72.  
  73. def setupStylesheet():
  74. #   function for setting up the stylesheet used to format data
  75. #   ----------------------------------------------------------------------------
  76.     stylesheet = open((os.path.join(os.getcwd(),"output\\css\\stylesheet.css")), "wt")
  77.     stylesheet.write('body {background-color:#ffffff;}\n')
  78.     stylesheet.write('table {font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width: 100%;border: 1px solid #cef;text-align: left;}\n')
  79.     stylesheet.write('th {font-weight: bold;background-color: #acf;border-bottom: 1px solid #cef; }\n')
  80.     stylesheet.write('td,th {padding: 4px 5px; }\n.odd {background-color: #def; } \n.odd td {border-bottom: 1px solid #cef; }')
  81.     stylesheet.close()
  82. #   ----------------------------------------------------------------------------
  83.  
  84.  
  85.  
  86. def userInput(a):
  87. #   this function will check to see if the value entered is viable ir not will loop
  88. #   the parameter is the option chosen on the menu
  89.     userOptions = [True,True,True,True,True,True,True,True,True]
  90.     if a == '1':
  91.         searchData(1)
  92.     elif a == '2':
  93.         searchData(2)
  94.     elif a == '3':
  95.         searchData(3)
  96.     elif a == '4':
  97.         searchData(4)
  98.     elif  a == '5':
  99.         options(userOptions)
  100.     elif a == '0':
  101.         exit()
  102.     else:
  103.         print('please enter a valid option')
  104.         userInput(input(message))
  105.  
  106.  
  107.  
  108. def searchData(a):
  109. #   code for searching through the data, the parameter is the choice of option from the user
  110. #   ----------------------------------------------------------------------------
  111.  
  112.     searchData_list = []    #create a list to store the matched data
  113.  
  114.     foundInRow = False;     #a boolean variable set up for use in the for loops
  115.  
  116.     if (a == 1):
  117.         searchFor = input("what would you lik to search for?:")      #ask the user what to search for
  118.         for row in data_list:
  119.             foundInRow = False
  120.             for cell in row:
  121.                 if (re.findall(searchFor ,cell , re.IGNORECASE)):
  122.                     foundInRow = True
  123.                     break
  124.             if (foundInRow):
  125.                 searchData_list.append(row)
  126.  
  127.  
  128.  
  129.     elif (a ==2):
  130.     # the nested for loops sort through each row and then each cell searching for exact data matches
  131.         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
  132.         for row in data_list:
  133.            foundInRow = False
  134.            for cell in row:
  135.                if (searchFor.lower() == cell.lower()): # to make searching easier made both the cell and the search lower case only to increase matches
  136.                    foundInRow = True
  137.            if (foundInRow):
  138.                searchData_list.append(row) #only if there was a match will the data be appended
  139.  
  140.  
  141.     elif (a == 3):
  142.          amount = float(input ('Enter how minimum expenditure:'))
  143.          for row in data_list:
  144.            foundInRow = False
  145.            if (amount <= float(re.sub(",","",row[7]))): #takes the sting of the amount
  146.                foundInRow = True
  147.            if (foundInRow):
  148.                searchData_list.append(row)
  149.  
  150.  
  151.     elif (a == 4):
  152.          amount = (float(input ('Enter how maximum expenditure:')))
  153.          for row in data_list:
  154.            foundInRow = False
  155.            if (amount >= float(re.sub(",","",data_list[row][7]))): #takes the sting of the amount
  156.                foundInRow = True
  157.            if (foundInRow):
  158.                searchData_list.append(row)
  159.  
  160.  
  161.     #This code tells the user if no data was found on their search and will ask if they want to try again
  162.     if (len(searchData_list) == 0):
  163.         answer = ' '
  164.         while(answer.lower() != 'y' and answer.lower() != 'n'):
  165.             answer = input("No data found would you like to try again? (Y/N)")
  166.         if (answer == 'n'):
  167.             exit()
  168.         if (answer == 'y'):
  169.             userInput(input(message))
  170.     print (searchData_list)
  171.  
  172.     createOutputFile(searchData_list)
  173. #   ----------------------------------------------------------------------------
  174.  
  175.  
  176. def createOutputFile(searchData_list):
  177. #   write the data as a HTML file
  178. #   ----------------------------------------------------------------------------
  179.         sortedData_list = sorted(searchData_list,key=lambda x:x[column])
  180.         output_file = open((os.path.join(os.getcwd(),"output\\results\\test.html")), "wt")
  181.         output_file.write('<html> \n' + '<head> \n')
  182.         output_file.write('<link rel="stylesheet" type="text/css" ')
  183.         output_file.write('href="'+(os.path.join(os.getcwd(),"output\\css\\stylesheet.css")) +'" /> \n')
  184.         output_file.write('</head> \n' +'<body>\n ' + '<table border="1">\n')
  185.  
  186.         #print(sortedData_list)
  187.         firstRow = 0
  188.         for row in searchData_list:
  189.            # for in range(1-100):
  190.  
  191.             output_file.write('<tr>')
  192. ##            if row == 0:
  193. ##                for cell in row:
  194. ##                    output_file.write('<th>' + cell + '</th>')
  195. ##            else:
  196.             for cell in row:
  197.                 output_file.write('<td>' + cell + '</td>')
  198.  
  199.  
  200.         output_file.write('</tr>')
  201.  
  202.         output_file.write('</table id="data">' + '</body>' + '</html>')
  203.         output_file.close()
  204.         input('Would you like to narrow down the search?:')
  205. #   ----------------------------------------------------------------------------
  206.  
  207.  
  208. def options(userOptions):
  209.     optionMessage = 'Enter the number to toggle options: \n'
  210.     optionMessage = optionMessage + '    1:Show "Department Family" in output\n'
  211.     optionMessage = optionMessage + '        Currently:' + boolToStr(userOptions[0]) + '\n'
  212.     optionMessage = optionMessage + '    2:Show "Entity" in output\n'
  213.     optionMessage = optionMessage + '        Currently:' + boolToStr(userOptions[1]) + '\n'
  214.     optionMessage = optionMessage + '    3:Show "Date Of Payment" in output\n'
  215.     optionMessage = optionMessage + '        Currently:' + boolToStr(userOptions[2]) + '\n'
  216.     optionMessage = optionMessage + '    4:Show "Expense Type" in output\n'
  217.     optionMessage = optionMessage + '        Currently:' + boolToStr(userOptions[3]) + '\n'
  218.     optionMessage = optionMessage + '    5:Show "Expense Area" in ouput\n'
  219.     optionMessage = optionMessage + '        Currently:' + boolToStr(userOptions[4]) + '\n'
  220.     optionMessage = optionMessage + '    6:Show "Supplier" in ouput\n'
  221.     optionMessage = optionMessage + '        Currently:' + boolToStr(userOptions[5]) + '\n'
  222.     optionMessage = optionMessage + '    7:Show "Transaction Number" in ouput\n'
  223.     optionMessage = optionMessage + '        Currently:' + boolToStr(userOptions[6]) + '\n'
  224.     optionMessage = optionMessage + '    8:Show "Amount In Sterlin" in ouput\n'
  225.     optionMessage = optionMessage + '        Currently:' + boolToStr(userOptions[7]) + '\n'
  226.     optionMessage = optionMessage + '    9:Change the amount of rows per output file\n'
  227.     optionMessage = optionMessage + '        Currently:' + boolToStr(userOptions[8]) + '\n'
  228.     optionMessage = optionMessage + '    0:Exit to main manu'
  229.  
  230.     option = (int(input(optionMessage)))
  231.     if (option > 0 and option < 9):
  232.         if userOptions[option-1] == True:
  233.             userOptions[option-1] = False
  234.         else:
  235.             userOptions[option-1] = True
  236.  
  237.     elif (option == 0):
  238.         userInput(input(message))
  239.     options(userOptions)
  240.  
  241.  
  242. def boolToStr(boolean):
  243.     if boolean == True:
  244.         return 'True'
  245.     else:
  246.         return 'False'
  247.  
  248.  
  249. def toFloat(stringy):
  250.     print(stringy)
  251.  
  252.  
  253. def exit():
  254.     sys.exit(0)
  255. if __name__ == '__main__':
  256.     main()
Add Comment
Please, Sign In to add comment