-1

code looks like this:

import stddraw
import sys
import stdio
#open the lexicon file
lexicon_input_file = open(sys.argv[1])
# copy the lexicon over from file
lexicon = lexicon_input_file.readlines()
#close the lexicon file
lexicon_input_file.close()
for i in range(len(lexicon)): #4000 words
    # breaks down entire lexicon into each element incremented from 0,1,2,3,4,..
    line_of_text = lexicon[i]
    line_without_newline = line_of_text.rstrip('\n')
print(lexicon)

terminal results look like this:

['the\n', 'be\n', 'and\n', 'of\n', 'a\n', 'in\n', 'to\n', 'have\n', 'it\n'.......

how do i get rid of \n from ends of elements. i tried rsplit but idk why it didnt work. im really new to coding!

Julien
  • 13,986
  • 5
  • 29
  • 53

2 Answers2

0

Hi and welcome to StackOverflow!

try lexicon.replace() ok, so per this thread, it looks like you need to use the "raw" character denoted by '\' or r()

So it should look like this.

lexicon.replace('\\n', "")

Let me know if this works!

Guar
  • 13
  • 6
0

This should remove the '\n':

lexicon = [i.strip('\n') for i in lexicon]
print(lexicon)
Zito Relova
  • 1,041
  • 2
  • 13
  • 32