Advertisement
Guest User

Untitled

a guest
May 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. """Code written by Harry Low on the 19th May 2019.
  2.  
  3. Prints peoples names' and the status of their account and donations to
  4. each band."""
  5. import os.path
  6. def tag_position(tag, lines):
  7. """Provides the index position of a tag within certain lines of code"""
  8. counter = 0
  9. line_index = 0
  10. tag_found = False
  11. while counter < len(lines):
  12. if lines[counter].strip().startswith(tag):
  13. line_index = counter
  14. tag_found = True
  15. counter += 1
  16. else:
  17. counter += 1
  18. if tag_found == False:
  19. return -1
  20. else:
  21. return line_index
  22. return line_index
  23.  
  24. def tagged_contents(tag_name, lines):
  25. """Returns the lines of data between specified start and end tags in file"""
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. def main():
  34. """Iterates through text files to retrieve data related to donation status.
  35.  
  36. Prints data and analsyses band names starting and ending with tags and returns
  37. amount donated to each band"""
  38. filename = None
  39. while filename is None:
  40. filename = input("Enter a file name: ")
  41. if not os.path.isfile(filename):
  42. print(filename, "DOES NOT EXIST!")
  43. filename = None
  44.  
  45. file_data = open(filename, encoding="utf-8")
  46. lines = file_data.readlines()
  47. file_data.close()
  48.  
  49. end_head = tag_position("</head>", lines)
  50. start_head = tag_position("<head>", lines)
  51. header_lines = lines[start_head+1:end_head]
  52. _, donor_name = header_lines[0].strip().split(':')
  53. _, donor_status = header_lines[1].strip().split(':')
  54.  
  55. start_data = tag_position("<data>", lines)
  56. end_data = tag_position("</data>", lines)
  57. data_lines = lines[start_data+1:end_data]
  58. data = []
  59. for line in data_lines:
  60. _, band_name, amount = line.strip().split(',')
  61. amount = float(amount)
  62. data.append((band_name, amount))
  63.  
  64. total_donations = 0
  65. for band_name, amount in data:
  66. total_donations += amount
  67.  
  68. print("-" * 40)
  69. print("Donation Summary")
  70. print("-" * 40)
  71. print("User name: {}".format(donor_name.title()))
  72. print("Account Status: {}".format(donor_status))
  73.  
  74. if donor_status in ["suspended", "deleted"]:
  75. print("*** WARNING ***")
  76. print("*** User can't access their account ***")
  77. print("-" * 40)
  78.  
  79. for band_name, amount in sorted(data):
  80. print("{} ({:.2f})".format(band_name, amount))
  81. print("-" * 40)
  82. print("Count: {}".format(len(data)))
  83. print("Total: {:.2f}".format(total_donations))
  84. print("-" * 40)
  85.  
  86. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement