1

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() 
3
  • 3
    Your code is eliminating the line breaks because it splits each line into words and then assembles a list of those words. So it is not preserving the line breaks in the input. Try sanat = syote.split(" ") + ["\n"]. But you will also have to make an exception for this so that it is not marked as an error. Commented Aug 13, 2021 at 10:46
  • 1
    Take a look at 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. Commented Aug 13, 2021 at 10:58
  • the list approach is quite tricky since to recover the multi-line string you need first the keep in mind the original positions of the line-breaks and then update them with the amount of misspell per line. If you are note subject to any restrictions use regular expressions! Commented Aug 13, 2021 at 12:25

4 Answers 4

1

Change

ready_list = ' '.join(new_list) 

to

ready_list = '\n'.join(new_list) 
Sign up to request clarification or add additional context in comments.

2 Comments

That puts every word in the output on a new line. It seems to me OP wants to preserve the line breaks in the input, not add new ones.
Thanks, but the \n should be in the exact place where it is in the original input, i.e. after "fox" and "dog", not after each word.
1

Try adding '\n' when joining the list elements:

ready_list = '\n'.join(new_list) 

This separates each string in the list with the newline-command.

1 Comment

Thanks, but the \n should be in the exact place where it is in the original input, i.e. after "fox" and "dog", not after each word.
1

The easiest way is to add '\n' in ready_list = ' '.join(new_list)

From: ready_list = ' '.join(new_list)

To: ready_list = '\n'.join(new_list)

1 Comment

Thanks, but the \n should be in the exact place where it is in the original input, i.e. after "fox" and "dog", not after each word.
1

Try the code below:

print(*ready_list,sep='\n') 

It will print each element in the list.

1 Comment

Thanks, but the \n should be in the exact place where it is in the original input, i.e. after "fox" and "dog", not after each word.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.