0

This is a follow on from a previous question.

I've loaded a word list in Python, but there is a problem. For example, when I access the 21st item in wordlist I should get "ABACK". Instead I get:

wordlist[21]
"'ABACK\\n',"

So for each word in wordlist I need to trim "'" off the front, and "\\n'," off the back of every string in wordlist. I've tried different string methods but haven't found one that works yet.

Community
  • 1
  • 1
user224530
  • 49
  • 1
  • 1
  • 6
  • 2
    `wordlist[21] = wordlist[21][1:-4]`? – jonrsharpe Apr 16 '15 at 09:46
  • @PeterWood note that `'\\n'` is a literal backslash followed by an 'n', not a newline. – jonrsharpe Apr 16 '15 at 10:02
  • Are you loading data to wordlist from the file? because the newline character is appending to the string, if so then can you mention how you are loading the wordlist. – Manjunath N Apr 16 '15 at 10:06
  • 1
    I strongly suspect that the problem is that you're processing the strings with `repr` somewhere while loading them (or maybe even while saving them, if you wrote the code that creates the file), in which case the right question is not "how do I undo `repr` on a string that I shouldn't have called it on", but "what should I be doing instead of calling `repr` here". – abarnert Apr 16 '15 at 10:42
  • So, with that in mind: please show us the code you've written to load the wordlist, and a sample wordlist file (just a few entries). – abarnert Apr 16 '15 at 10:44

3 Answers3

0
wordlist = [ e [1:-4] for e in wordlist]

That does the trick. Courtesy of whoever commented above and a similar post.

user224530
  • 49
  • 1
  • 1
  • 6
0

Since there is a backslash before \n, doing a simple strip won't work if you want to remove it. You can do this:

wordlist = [word.strip("'").strip().split("\\n")[0] for word in wordlist]

Additional strip if you actually have a \n to get rid of. Or you can do word[1:-4] as @jonrsharpe has suggested.

web_ninja
  • 2,351
  • 3
  • 22
  • 43
0

If this is generic to every word in list you can utilize lstrip() and rstrip() functions.

for word in wordlist:
   word.lstrip("'").rstrip("\\n',")
shaktimaan
  • 1,769
  • 13
  • 14
  • I thought about that, but what if the word itself ends with `'n'`? – jonrsharpe Apr 16 '15 at 10:17
  • What ever charachter you want to strip you can pass to the function, in this case it would be word.rstrip('n'). And if you are asking about '\n' charachter then you can mention the string with r' '(this suppresses the meaning of \, so you can specify with out these \\ e.g. word.rstrip(r'\n') has same effect as word.rstrip('\\n')). Does this answers your question or were you asking something else....? – shaktimaan Apr 16 '15 at 10:36
  • No, I mean your current approach would strip the `'n'` **but shouldn't**. Try e.g. `"run\\n',".rstrip("\\n',")`. I don't think you fully understand what `str.[l/r]strip` does. – jonrsharpe Apr 16 '15 at 10:37
  • Oh I did not think about that test case. Well in this case you can use chaining to it like this: "run\\n".rstrip("n").rstrip("\\"). Here main problem comes with the case that "\\n" is a part of string so it is treated as "\" and not "\n", thats why "run\\n".rstrip(r"\n") first stripeed "\\" and then "n". It did not treat it as "\n" at all. – shaktimaan Apr 16 '15 at 10:58
  • Of course it didn't - in `\\n` the first backslash escapes the second, making it a literal backslash, and the n is just an n. It isn't a newline. – jonrsharpe Apr 16 '15 at 10:59