Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import plotly.plotly as py
- import plotly.graph_objs as go
- import collections
- import xml.etree.ElementTree as ET
- from datetime import datetime
- from operator import itemgetter
- # Beautifultable - do drukowania danych tabelarycznych do terminala.
- from beautifultable import BeautifulTable
- # sortowanie - dodatkowo
- from natsort import natsorted
- # ElementTree reprezentuje cały dokument XML jako drzewo,
- # a Element reprezentuje pojedynczy węzeł w tym drzewie, root jest obiektem Element
- tree = ET.parse('Posts.xml')
- root = tree.getroot()
- table = BeautifulTable()
- table.column_headers = ["GODZINA", "ILOŚĆ"]
- godziny = []
- for row in root:
- postTime = row.attrib['CreationDate']
- # strptime() - %Y Year with century as a decimal number.
- # %m Month as a zero-padded decimal number.
- # %d Day of the month as a zero-padded decimal number.
- # %H Hour (24-hour clock) as a zero-padded decimal number.
- # %M Minute as a zero-padded decimal number.
- # %S Second as a zero-padded decimal number.
- # %f Microsecond as a decimal number, zero-padded on the left.
- postTime_object = datetime.strptime(postTime, '%Y-%m-%dT%H:%M:%S.%f')
- if (str(postTime_object.year) == '2017'):
- godziny.append(str(postTime_object.hour))
- wszystkie = collections.Counter(godziny)
- wynik = wszystkie.most_common(24)
- for x, y in wynik:
- table.append_row([x,y]) # wyswietlanie tabelarycze
- print("Rozkład godzin publikacji postów w roku 2017 (liczony co godzinę, tj. <0-1), <1-2), (..), <22-23), <23-0)):")
- print(table)
- posortowane = collections.OrderedDict(sorted(wynik))
- plt.ylabel('Ilosc wystapien')
- plt.xlabel('Godzina')
- plt.bar(posortowane.keys(), posortowane.values(), color='g')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement