0

I am making a dictionary application using argparse in Python 3. I'm using difflib to find the closest matches to a given word. Though it's a list, and it has newline characters at the end, like:

['hello\n', 'hallo\n', 'hell\n']

And when I put a word in, it gives a output of this:

hellllok could be spelled as hello
hellos
hillock

Question:

I'm wondering if there is a reverse or inverse \n so I can counteract these \n's.

Any help is appreciated.

JBoy Advance
  • 78
  • 1
  • 13
  • 3
    Why not just remove the `\n`? – Carcigenicate Jan 16 '18 at 00:55
  • I can't manipulate the difflib list, so I don't have any control over that. If I could I would've already. – JBoy Advance Jan 16 '18 at 00:56
  • Well, in that case even if there was a "reverse newline", you couldn't use it here. Without context, I can't say for sure, but you can almost definitely just remove the newline off the string. You'll need to make a copy of the list of strings, but you'd have to do that anyway to add a "reverse newline" character. – Carcigenicate Jan 16 '18 at 00:58
  • If you have the ability to append a "reverse newline" to the end of each string, then you are able to control the content. If you can't append it, then it won't help you. – William Pursell Jan 16 '18 at 00:59
  • 1
    Where do you get the list in the first place? I assume you're using `difflib.get_close_matches`; what do you put as the second argument? Strip *those* strings. – Amadan Jan 16 '18 at 01:02
  • I use ```difflib.get_close_matches``` yes. And I used ```dictionary``` as the second argument. (```dictionary = open('/usr/share/dict/american-english', 'r')```) – JBoy Advance Jan 16 '18 at 01:05
  • 1
    Just use `.strip()` to remove any whitespace characters from the beginning and ending of a string. – Klaus D. Jan 16 '18 at 01:13

1 Answers1

4

There's no "reverse newline" in the standard character set but, even if there was, you would have to apply it to each string in turn.

And, if you can do that, you can equally modify the strings to remove the newline. In other words, create a new list using the current one, with newlines removed. That would be something like:

>>> oldlist = ['hello\n', 'hallo\n', 'hell\n']
>>> oldlist
['hello\n', 'hallo\n', 'hell\n']
>>> newlist = [s.replace('\n','') for s in oldlist]
>>> newlist
['hello', 'hallo', 'hell']

That will remove all newlines from each of the strings. If you want to ensure you only replace a single newline at the end of the strings, you can instead use:

newlist = [re.sub('\n$','',s) for s in oldlist]
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • So you're saying that I can put the output of the difflib into a variable, and use ```[i.replace('\n','') for i in x]``` for that variable? – JBoy Advance Jan 16 '18 at 01:03
  • @JBoyAdvance: yes, that's exactly what I'm saying. I've updated the code snippet to show how to store it in a new variable, one you can use insted of the original. – paxdiablo Jan 16 '18 at 01:05
  • Easier than stripping difflib output, strip its _input_: `dictionary = (word.strip() for word in open('/usr/share/dict/american-english'))`. – Amadan Jan 19 '18 at 02:14