SHARE
TWEET

Untitled

a guest Jul 17th, 2017 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1
  2. 1
  3. 1
  4. 1
  5. 1
  6. 1
  7. 1
  8. 1
  9. 1
  10. 1
  11. 6
  12. 8
  13. 6
  14. 9
  15. 9
  16. 9
  17. 9
  18. 9
  19. 9
  20. 9
  21. 9
  22. 9
  23. 9
  24. 9
  25. 9
  26. 9
  27. 9
  28. 9
  29. 9
  30. 9
  31. 9
  32. 9
  33. 9
  34. 9
  35. 9
  36. 9
  37. 9
  38. 9
  39. 9
  40. 9
  41. 9
  42. 9
  43. 9
  44. 9
  45. 9
  46. 9
  47. 9
  48. 9
  49. 9
  50. 9
  51. 9
  52. 9
  53. 9
  54. 9
  55. 9
  56. 9
  57. 9
  58. 9
  59. 9
  60. 9
  61. 9
  62. 9
  63. 9
  64. 9
  65. 9
  66. 9
  67. 9
  68.    
  69. import pyodbc
  70. import pprint
  71. import re
  72.  
  73. """
  74. DATABASE CONNECTION BELOW
  75. """
  76. conn_string = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:Userscqt7wnyDocumentsUPS.accdb;'
  77. conn = pyodbc.connect(conn_string)
  78. cur = conn.cursor()
  79.  
  80. cur.execute("SELECT * FROM Main_Setup order by ID");
  81. rows = cur.fetchall()
  82.  
  83. n2600RA1 = []
  84. n2600RA2 = []
  85. n2600RA3 = []
  86. n2600RA4 = []
  87. n2600RA5 = []
  88. n2601CV4 = []
  89. n2601IV4 = []
  90. n2601CV1 = []
  91. aTL10GV1 = []
  92. bEL10GV1 = []
  93. cHL10GV1 = []
  94. cZL10GV1 = []
  95. dEL10GV1 = []
  96. dKL10GV1 = []
  97. eSL10GV1 = []
  98. fIL10GV1 = []
  99. fRL10GV1 = []
  100. gBL10GV1 = []
  101. hUL10GV1 = []
  102. iEL10GV1 = []
  103. iTL10GV1 = []
  104. nLL10GV1 = []
  105. nOL10GV1 = []
  106. pLL10GV1 = []
  107. pTL10GV1 = []
  108. sEL10GV1 = []
  109. n2601CW3 = []
  110.  
  111. job_start = []
  112. job_end = []
  113. names = []
  114. name = None
  115. pk = []
  116.  
  117. for row in rows:
  118.     name = row[1]
  119.     fields = row[4].split(',')
  120.     start = row[2]
  121.     end = row[3]
  122.     prim = row[0]
  123.  
  124.     if name == 'JOB - N2600RA1':
  125.         n2600RA1.extend(fields)
  126.         job_start.append(start)
  127.         job_end.append(end)
  128.         pk.append(prim)
  129.         names.append(name)
  130.  
  131.     elif name == 'JOB - N2600RA2':
  132.         n2600RA2.extend(fields)
  133.         job_start.append(start)
  134.         job_end.append(end)
  135.         pk.append(prim)
  136.         names.append(name)
  137.  
  138.     elif name == 'JOB - N2600RA3':
  139.         n2600RA3.extend(fields)
  140.         job_start.append(start)
  141.         job_end.append(end)
  142.         pk.append(prim)
  143.         names.append(name)
  144.  
  145.     elif name == 'JOB - N2600RA4':
  146.         n2600RA4.extend(fields)
  147.         job_start.append(start)
  148.         job_end.append(end)
  149.         pk.append(prim)
  150.         names.append(name)
  151.  
  152.     elif name == 'JOB - N2600RA5':
  153.         n2600RA5.extend(fields)
  154.         job_start.append(start)
  155.         job_end.append(end)
  156.         pk.append(prim)
  157.         names.append(name)
  158.  
  159.     elif name == 'JOB - N2601CV4':
  160.         n2601CV4.extend(fields)
  161.         job_start.append(start)
  162.         job_end.append(end)
  163.         pk.append(prim)
  164.         names.append(name)
  165.  
  166.     elif name == 'JOB - N2601IV4':
  167.         n2601IV4.extend(fields)
  168.         job_start.append(start)
  169.         job_end.append(end)
  170.         pk.append(prim)
  171.         names.append(name)
  172.  
  173.     elif name == 'JOB - N2601CV1':
  174.         n2601CV1.extend(fields)
  175.         job_start.append(start)
  176.         job_end.append(end)
  177.         pk.append(prim)
  178.         names.append(name)
  179.  
  180.     elif name == 'JOB - ATL10GV1':
  181.         aTL10GV1.extend(fields)
  182.         job_start.append(start)
  183.         job_end.append(end)
  184.         pk.append(prim)
  185.         names.append(name)
  186.  
  187.     elif name == 'JOB - BEL10GV1':
  188.         bEL10GV1.extend(fields)
  189.         job_start.append(start)
  190.         job_end.append(end)
  191.         pk.append(prim)
  192.         names.append(name)
  193.  
  194.     elif name == 'JOB - CHL10GV1':
  195.         cHL10GV1.extend(fields)
  196.         job_start.append(start)
  197.         job_end.append(end)
  198.         pk.append(prim)
  199.         names.append(name)
  200.  
  201.     elif name == 'JOB - CZL10GV1':
  202.         cZL10GV1.extend(fields)
  203.         job_start.append(start)
  204.         job_end.append(end)
  205.         pk.append(prim)
  206.         names.append(name)
  207.  
  208.     elif name == 'JOB - DEL10GV1':
  209.         dEL10GV1.extend(fields)
  210.         job_start.append(start)
  211.         job_end.append(end)
  212.         pk.append(prim)
  213.         names.append(name)
  214.  
  215.     elif name == 'JOB - DKL10GV1':
  216.         dKL10GV1.extend(fields)
  217.         job_start.append(start)
  218.         job_end.append(end)
  219.         pk.append(prim)
  220.         names.append(name)
  221.  
  222.     elif name == 'JOB - ESL10GV1':
  223.         eSL10GV1.extend(fields)
  224.         job_start.append(start)
  225.         job_end.append(end)
  226.         pk.append(prim)
  227.         names.append(name)
  228.  
  229.     elif name == 'JOB - FIL10GV1':
  230.         fIL10GV1.extend(fields)
  231.         job_start.append(start)
  232.         job_end.append(end)
  233.         pk.append(prim)
  234.         names.append(name)
  235.  
  236.     elif name == 'JOB - FRL10GV1':
  237.         fRL10GV1.extend(fields)
  238.         job_start.append(start)
  239.         job_end.append(end)
  240.         pk.append(prim)
  241.         names.append(name)
  242.  
  243.     elif name == 'JOB - GBL10GV1':
  244.         gBL10GV1.extend(fields)
  245.         job_start.append(start)
  246.         job_end.append(end)
  247.         pk.append(prim)
  248.         names.append(name)
  249.  
  250.     elif name == 'JOB - HUL10GV1':
  251.         hUL10GV1.extend(fields)
  252.         job_start.append(start)
  253.         job_end.append(end)
  254.         pk.append(prim)
  255.         names.append(name)
  256.  
  257.     elif name == 'JOB - IEL10GV1':
  258.         iEL10GV1.extend(fields)
  259.         job_start.append(start)
  260.         job_end.append(end)
  261.         pk.append(prim)
  262.         names.append(name)
  263.  
  264.     elif name == 'JOB - ITL10GV1':
  265.         iTL10GV1.extend(fields)
  266.         job_start.append(start)
  267.         job_end.append(end)
  268.         pk.append(prim)
  269.         names.append(name)
  270.  
  271.     elif name == 'JOB - NLL10GV1':
  272.         nLL10GV1.extend(fields)
  273.         job_start.append(start)
  274.         job_end.append(end)
  275.         pk.append(prim)
  276.         names.append(name)
  277.  
  278.     elif name == 'JOB - NOL10GV1':
  279.         nOL10GV1.extend(fields)
  280.         job_start.append(start)
  281.         job_end.append(end)
  282.         pk.append(prim)
  283.         names.append(name)
  284.  
  285.     elif name == 'JOB - PLL10GV1':
  286.         pLL10GV1.extend(fields)
  287.         job_start.append(start)
  288.         job_end.append(end)
  289.         pk.append(prim)
  290.         names.append(name)
  291.  
  292.     elif name == 'JOB - PLL10GV1':
  293.         pLL10GV1.extend(fields)
  294.         job_start.append(start)
  295.         job_end.append(end)
  296.         pk.append(prim)
  297.         names.append(name)
  298.  
  299.     elif name == 'JOB - PTL10GV1':
  300.         pTL10GV1.extend(fields)
  301.         job_start.append(start)
  302.         job_end.append(end)
  303.         pk.append(prim)
  304.         names.append(name)
  305.  
  306.     elif name == 'JOB - SEL10GV1':
  307.         sEL10GV1.extend(fields)
  308.         job_start.append(start)
  309.         job_end.append(end)
  310.         pk.append(prim)
  311.         names.append(name)
  312.  
  313.     elif name == 'JOB - N2601CW3':
  314.         n2601CW3.extend(fields)
  315.         job_start.append(start)
  316.         job_end.append(end)
  317.         pk.append(prim)
  318.         names.append(name)
  319.  
  320. """
  321. DATABASE CONNECTION ENDED
  322. """
  323.  
  324. outFileName = "out3.txt"
  325.  
  326. # The regex pattern that is used to extract timestamp from file
  327. # it will search for timestamps like this 2017-06-13-22.31.30.978293
  328. dateRegEx = r"[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}.[0-9]{2}.[0-9]{2}.[0-9]+"
  329. # Compile the pattern
  330. regExPtrn = re.compile(dateRegEx)
  331.  
  332.  
  333. # Extract the current job fields
  334. def field_Extract(fileLines, fieldsArray, delimit):
  335.     # Empty string in which we will append the
  336.     # extracted fields
  337.     matchStr = ""
  338.     for line in fileLines:
  339.         for field in fieldsArray:
  340.             if line.startswith(field):
  341.                 key, value = line.split(delimit)
  342.                 matchStr += key.strip() + "tt : " + value.strip() + "n"
  343.     # Return the string hold the extracted fields
  344.     # Each field is onn a separate line
  345.     return matchStr
  346.  
  347.  
  348. # Open input and output files
  349. test_file = open(r'C:Userscqt7wnyDesktopSAVERS_REPT_DT0712.txt', 'r+')
  350. outFile = open(outFileName, 'w')
  351.  
  352. # Initialize used variables
  353. currentJob = -1
  354. currentJobData = []
  355. startAppending = False
  356. outFileStr = ""
  357. for line in test_file:
  358.  
  359.     # Get the current job
  360.     # We will loop over each job start and check if this start is
  361.     # contained in the current line, if so, this line will be considered
  362.     # the first line of the job
  363.     for jobStart in job_start:
  364.         if jobStart in line:
  365.             # the used search function return the index of the first
  366.             # match of the string. If the substring has more than one
  367.             # occurance in the searchable string, only the index of
  368.             # the first occurance will be returned
  369.             currentJob = job_start.index(jobStart) + 1
  370.             # Set the flag to start gathering job lines
  371.             # for each job we will apped alll its lines to an empty
  372.             # list, so we have all job lines separated
  373.             startAppending = True
  374.  
  375.     # If job start found, gathar job lines
  376.     if startAppending == True:
  377.         currentJobData.append(line)
  378.  
  379.     # Set the correct job
  380.     if currentJob == 1:
  381.         job = n2600RA1
  382.     elif currentJob == 2:
  383.         job = n2600RA2
  384.     elif currentJob == 3:
  385.         job = n2600RA3
  386.     elif currentJob == 4:
  387.         job = n2600RA4
  388.     elif currentJob == 5:
  389.         job = n2600RA5
  390.     elif currentJob == 6:
  391.         job = n2601CV4
  392.     elif currentJob == 7:
  393.         job = n2601IV4
  394.     elif currentJob == 8:
  395.         job = n2601CV1
  396.     #elif currentJob == 9:
  397.      #   job = aTL10GV1
  398.     elif currentJob == 9:
  399.         job = n2601CW3
  400.     else:
  401.         currentJob = -1
  402.  
  403.     # Check job end
  404.     # We will loop over each job end and check if this end is
  405.     # contained in the current line, if so, this line will be considered
  406.     # the last line of the job
  407.     for jobEnd in job_end:
  408.         # Check that this is a valid job and that the job ending
  409.         # string is contained in the current line
  410.         if (currentJob != -1) and (jobEnd in line):
  411.             print(currentJob)
  412.             # As a job end found, stop gathering lines
  413.             startAppending = False
  414.             # Get the time stamp
  415.             # We search for it in the currnet line using the previously
  416.             # compiled regex pattern
  417.             txt = "".join(currentJobData)
  418.             # Find all occurance of timestamps on the current job lines
  419.             #timeStamp = regExPtrn.findall(txt)
  420.             # Check that a timestamp found
  421.             #if len(timeStamp) >= 1:
  422.                 # If there is more than one timestamp in the current
  423.                 # job lines, get only the first one
  424.                 #timeStamp = timeStamp[0]
  425.             # Append the found output to the output string
  426.             outFileStr += '########============ NEW JOB STARTS HERE ===========#########'
  427.             outFileStr += "n"
  428.             outFileStr += "job# " + str(name[currentJob])
  429.             outFileStr += "n"
  430.             #outFileStr += "Timestamp: " + timeStamp
  431.             outFileStr += "n"
  432.             # Extract job fields values
  433.             outFileStr += field_Extract(currentJobData, job, ':')
  434.             # Erase completed job lines to be used with the next job
  435.             currentJobData = []
  436.             # Set job to invalid job
  437.             currentJob = -1
  438.  
  439. # Write the output to output file
  440. outFile.write(outFileStr)
  441. # Close opened files
  442. outFile.close()
  443. test_file.close()
RAW Paste Data
Top