I have a program which spellchecks the input words from a text file and then adds asterisks around the words with typos. The input is like this:
The quick brown fox jumps over the lazy dog and mispells someting However, the print in the end looks like this:
*The* quick brown fox jumps over the lazy dog and *mispells* *someting* How can I add the "\n" in the right places? The code is here:
def main(): lahtotiedosto = open("words.txt", "r") list = [] input_list = [] for rivi in lahtotiedosto: rivi = rivi.rstrip() list.append(rivi) print("Enter the text to be spell-checked (empty line to end input).") typos = 0 syote = input() while syote != "": input_list.append(syote) syote = input() new_list = [] for syote in input_list: sanat = syote.split(" ") for i in sanat: if i in list: uusi_lista.append(i) else: i = "*{:}*".format(i) new_list.append(i) typos += 1 print("Checked text, typos highlighted with '*'") ready_list = ' '.join(new_list) print(ready_list) print("There were", typos, "typos.") main()
sanat = syote.split(" ") + ["\n"]. But you will also have to make an exception for this so that it is not marked as an error.str.splitlines(True)which will split the string into a list, retaining the newline. Then you will need to traverse each element in the list allowing for a newline at the end, with the possible exception of the last item.