Python 记事本++;脚本:如何删除换行符?
我目前正在用PythonScript编写一个非常简单的脚本,用于文本文件中的基本ASCII复选框支持 计划是,当我点击Alt+F2时,如果行以复选框开头,编辑器将[]切换到[x],并将[x]切换到[],否则只在当前位置插入[] 我写了一个剧本,它很有效。。。几乎Python 记事本++;脚本:如何删除换行符?,python,scripting,notepad++,Python,Scripting,Notepad++,我目前正在用PythonScript编写一个非常简单的脚本,用于文本文件中的基本ASCII复选框支持 计划是,当我点击Alt+F2时,如果行以复选框开头,编辑器将[]切换到[x],并将[x]切换到[],否则只在当前位置插入[] 我写了一个剧本,它很有效。。。几乎 from Npp import * import string # If the line starts with [ ] or [x] the script toggles the value between the two pos
from Npp import *
import string
# If the line starts with [ ] or [x] the script toggles the value between the two possibilites
# if the line doesn't contains [ ] the script adds the empty box at the current position
curLine = editor.getCurLine()
curPos = editor.getCurrentPos()
curLineNr = editor.lineFromPosition(curPos)
strippedLine = curLine.lstrip()
if (strippedLine.startswith('[ ]')):
curLine = curLine.replace('[ ]', '[x]', 1).rstrip('\n')
editor.replaceWholeLine(curLineNr, curLine)
editor.gotoPos(curPos)
elif (strippedLine.startswith('[x]')):
curLine = curLine.replace('[x]', '[ ]', 1).rstrip('\n')
editor.replaceWholeLine(curLineNr, curLine)
editor.gotoPos(curPos)
else:
editor.addText('[ ] ')
但是这个脚本在替换的编辑器行之后添加了一个换行符。一个非常愚蠢的工作是删除新插入的行,但我不想首先插入它
编辑:/使其正常工作。只需使用editor.replaceWholeLine方法,它就会像一个符咒一样工作。将editor.replaceWholeLine方法与editor.replaceLine方法一起使用
上面的脚本现在起作用了如果你解决了它,为什么不发布你自己问题的答案呢?然后我们可以考虑解决的问题。