generate functionTue, 27 Dec 2022

Read a text file line by line and display each word separated by a #.

def read_lines(file_path): with open(file_path, "r") as file: content = file.readlines() file.close() return content def remove_new_line(string): return string.replace("\n", "") def remove_empty_spaces(string): return string.replace(" ", "") def display_words(string): return string.replace(" ", "#") def main(): file_path = "./file.txt" for line in read_lines(file_path): line = remove_new_line(line) line = remove_empty_spaces(line) print(display_words(line)) if __name__== "__main__": main()

Questions about programming?Chat with your personal AI assistant