Advertisement
Guest User

Untitled

a guest
Dec 10th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1.  
  2. # Get the filepath from the command line
  3. import sys
  4. P= sys.argv[1]
  5. F= sys.argv[2]
  6. L= sys.argv[3]
  7. B= sys.argv[4]
  8.  
  9. # ----------------------------------------------------------------
  10. #
  11. # Our Helper functions:
  12. #
  13. # ----------------------------------------------------------------
  14.  
  15. #
  16. # Loads the file at filepath
  17. # Returns a 2d array with the data
  18. #
  19. def load2dArrayFromFile(filepath):
  20. file = open(filepath,'r')
  21. contents = file.read()
  22. file.close()
  23. output = []
  24. S = contents.split('\n')
  25. for X in range(len(S)):
  26. output.append(S[X].split('|'))
  27. return output
  28. #
  29. # Searches the 2d array 'records' for firstname, lastname.
  30. # Returns the index of the record or -1 if no record exists
  31. #
  32. def findIndex(records, firstname, lastname):
  33. for X in range(len(records)):
  34. if records[X][0] == firstname and records[X][1] == lastname:
  35. return X
  36. return -1
  37.  
  38. # Sets the birthday of the record at the given index
  39. # Returns: nothing
  40. def setBirthday(records, index, newBirthday):
  41. records[index][2] = newBirthday
  42.  
  43. # Convert the 2d array back into a string
  44. # Return the text of the 2d array
  45. def makeTextFrom2dArray(records):
  46. output = []
  47. for X in range(len(records)):
  48. outputTMP = ('|').join(records[X])
  49. output.append(outputTMP)
  50. output = ('\n').join(output)
  51.  
  52. # ----------------------------------------------------------------
  53. #
  54. # Our main code body, where we call our functions.
  55. #
  56. # ----------------------------------------------------------------
  57.  
  58. # Load our records from the file into a 2d array
  59. records= load2dArrayFromFile(P)
  60.  
  61. # Find out which index, if any, has the name we are hunting
  62. indexWeAreHunting= findIndex(records, F, L)
  63.  
  64. # Set the birthday record to the one we were passed
  65. setBirthday(records, indexWeAreHunting, B)
  66.  
  67. # Convert the records into a text string
  68. output= makeTextFrom2dArray(records)
  69.  
  70. # Your code goes here
  71. # write the text string out to the file
  72. file = open(P,'w')
  73. file.write(output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement