有反\n吗?

Is there a reverse \n?

我正在 Python 中使用 argparse 制作字典应用程序 3. 我正在使用 difflib 查找与给定单词最接近的匹配项。虽然它是一个列表,但末尾有换行符,例如:

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

当我输入一个词时,它给出了这样的输出:

hellllok could be spelled as hello
hellos
hillock

问题:

我想知道是否存在反向或反向 \n 以便我可以抵消这些 \n

感谢任何帮助。

标准字符集中没有 "reverse newline",但即使有,您也必须依次将其应用于每个字符串。

而且,如果可以的话,您同样可以修改字符串以删除 换行符。换句话说,使用当前列表创建一个 new 列表,并删除换行符。那将是这样的:

>>> 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']

这将从每个字符串中删除 所有 换行符。如果你想确保只替换字符串 end 处的单个换行符,你可以改用:

newlist = [re.sub('\n$','',s) for s in oldlist]