I have a desk top file (paragraphs of words, numbers and punctuation). I need to get just the numbers, sort them, print sorted list, length and median. I can open, read and extract just the numbers but all numbers (even my double digits) print on "\n". I am allowed to use import statistics.
I have tried several attempts. I know I need a .split(),.strip() and .append. I just cannot figure out where.
from statistics import median
def sorting_file():
with open('C:\\Users\\wildk\\Desktop\\Numbers_in_text.txt', 'r') as f:
for line in f:
words = line.split().strip('n')
for i in words:
for number in i:
number = number.rstrip('n')
if(number.isnumeric()):
print(number)
def get_median():
for median in number:
number = sorted
median = sorted (number) [len(number)//2]
print(median, len )
if __name__=='__main__':
sorting_file()
get_median()
The
get_medianfunction has several problems:number, but that is an undefined name.numberassorted, which is a function. (Nothing gets sorted here)sorted(number)is then the same assorted(sorted)which makes no sense.sortedcall would work, you could not index it, as it is an iterator, not a list.print(number, len)would print two functions. I don't see how that is useful.medianfunction fromstatisticsso you don't needget_medianat all.Then
sorting_filehas problems too:.strip('n')on a list. Lists don't have such a method, and it is a mystery why you would want something to happen with the letternanywayfor number in iis iterating the individual characters of a word (i). This is not useful. The word itself is what you are interested in..rstrip('n')seems to serve no purposeHere is a correction that takes the above points into account: