Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def default_input(message, default_value):
- text = input(message)
- if text == "":
- return default_value
- else:
- return text
- in_file = default_input("Enter the input file name: [Press ENTER for waltham.txt]", "waltham.txt")
- line_len = default_input("Enter the line length: [Press ENTER for 50]", "50")
- search_word = default_input("Enter a search word: [Press ENTER for watch]", "watch")
- justification = default_input("Output justification (L/C/R): [Press ENTER for r]", "r")
- print("File Written")
- out_file = open(in_file.split(".")[0] + "_output.txt", "w")
- with open(in_file) as file:
- total_sent = 0
- total_word = 0
- total_char = 0
- total_digit = 0
- total_year = 0
- total_searched_word = 0
- lines = []
- for line in file:
- total_sent += len(line.split(". "))
- total_word += len(line.split(" "))
- total_char += len(line)
- for l in line.split(". "):
- if len(l) <= int(line_len):
- lines.append(l)
- for ch in line:
- if ch.isdigit():
- total_digit += 1
- for w in line.split(" "):
- if search_word in w:
- total_searched_word += 1
- if w.isdigit():
- total_year += 1
- avg_sent_len = total_char/total_sent
- out_file.writelines("File Analysis" + "\n\n")
- out_file.writelines(f'Sentences: {total_sent} Words: {total_word}\n')
- out_file.writelines(f'Characters: {total_char} Digits: {total_digit}\n')
- out_file.writelines(f'Years: {total_year} Average Sentence Length: {avg_sent_len}\n')
- out_file.writelines("Original Text:\n")
- ln = 1
- new_list = []
- for l in lines:
- out_file.writelines(f'{ln} {l}.\n')
- temp = l.split(" ")
- t = ""
- for e in temp:
- if e[0] != "[" and e[-1] != "]":
- t = t + e + " "
- new_list.append(t)
- ln += 1
- out_file.writelines("\n\nNew Text:\n")
- lno = 1
- for l in new_list:
- out_file.writelines(f'{lno} {l}.\n')
- lno += 1
- out_file.close()
Add Comment
Please, Sign In to add comment