Last active
March 12, 2020 09:17
-
-
Save elmoiv/458d8cb420ad766f9c3a78f2551bf21b to your computer and use it in GitHub Desktop.
Count repeated numbers in any file [plain-text and binary]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re, mimetypes | |
def extract(file_name): | |
get_type = mimetypes.guess_type(file_name)[0] | |
type = 'rb' if get_type != 'text/plain' else 'r' | |
unicode = 'utf-8' if type == 'r' else None | |
raw_data = str(open(file_name, type, encoding=unicode).read()) | |
return re.findall(r'\d+\.*\d*', raw_data) | |
def counter(extracted_data): | |
extracted_data = [(i, extracted_data.count(i)) for i in extracted_data] | |
sorted_data = sorted(extracted_data, key = lambda i: i[1])[::-1] | |
return dict(sorted_data) | |
def printer(data_dict): | |
center_len = len(max(data_dict, key = len)) + 10 | |
print( | |
'Number'.center(center_len), | |
'|', | |
'Count'.center(center_len)) | |
for number in data_dict: | |
print( | |
number.center(center_len), | |
'|', | |
str(data_dict[number]).center(center_len)) | |
def main(): | |
file_name = input('Enter File Name: ') | |
try: | |
open(file_name) | |
except FileNotFoundError: | |
raise Exception('File does not exist!') | |
print('Searching...') | |
search = extract(file_name) | |
if search: | |
print('Counting...') | |
counted = counter(search) | |
print('Printing...\n') | |
printer(counted) | |
return | |
print('File contains no numbers!') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment