Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import plotly.plotly as py
  3. import plotly.graph_objs as go
  4. import collections
  5. import xml.etree.ElementTree as ET
  6. from datetime import datetime
  7. from operator import itemgetter
  8. # Beautifultable - do drukowania danych tabelarycznych do terminala.
  9. from beautifultable import BeautifulTable
  10. # sortowanie - dodatkowo
  11. from natsort import natsorted
  12.  
  13. # ElementTree reprezentuje cały dokument XML jako drzewo,
  14. # a Element reprezentuje pojedynczy węzeł w tym drzewie, root jest obiektem Element
  15.  
  16. tree = ET.parse('Posts.xml')
  17. root = tree.getroot()
  18. table = BeautifulTable()
  19. table.column_headers = ["GODZINA", "ILOŚĆ"]
  20. godziny = []
  21.  
  22. for row in root:
  23. postTime = row.attrib['CreationDate']
  24. # strptime() - %Y Year with century as a decimal number.
  25. # %m Month as a zero-padded decimal number.
  26. # %d Day of the month as a zero-padded decimal number.
  27. # %H Hour (24-hour clock) as a zero-padded decimal number.
  28. # %M Minute as a zero-padded decimal number.
  29. # %S Second as a zero-padded decimal number.
  30. # %f Microsecond as a decimal number, zero-padded on the left.
  31. postTime_object = datetime.strptime(postTime, '%Y-%m-%dT%H:%M:%S.%f')
  32.  
  33. if (str(postTime_object.year) == '2017'):
  34. godziny.append(str(postTime_object.hour))
  35.  
  36.  
  37. wszystkie = collections.Counter(godziny)
  38. wynik = wszystkie.most_common(24)
  39.  
  40. for x, y in wynik:
  41. table.append_row([x,y]) # wyswietlanie tabelarycze
  42.  
  43.  
  44. print("Rozkład godzin publikacji postów w roku 2017 (liczony co godzinę, tj. <0-1), <1-2), (..), <22-23), <23-0)):")
  45. print(table)
  46.  
  47. posortowane = collections.OrderedDict(sorted(wynik))
  48. plt.ylabel('Ilosc wystapien')
  49. plt.xlabel('Godzina')
  50. plt.bar(posortowane.keys(), posortowane.values(), color='g')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement