[英]Append a line to previous in Notepad++ with Python script
I have a text file which contains lines with tab-indented and not indened. 我有一个文本文件,其中包含使用制表符缩进而不是缩进的行。 It looks like: 看起来像:
A a1,asdf,lkjhj
some thing here
B MORE THINGS,HERE
C MORE TEXTS HERE
HERE ALSO TEXTS
AND SO
I want to join indented lines with previous ones. 我想加入以前的缩进线。
The result should look like: 结果应如下所示:
A a1,asdf,lkjhj some thing here
B MORE THINGS,HERE
C MORE TEXTS HERE HERE ALSO TEXTS AND SO
As the file have more than 22,000 lines I tried to automate with a Python script using the notepad++ module Npp
. 由于该文件有超过22,000行,我尝试使用notepad ++模块Npp
自动化Python脚本。 I tried this: 我试过这个:
import sys
from Npp import *
notepad.open("input.txt")
i= 0
line=editor.gotoLine(i)
if line.startsWith('^[\t]' ) :
notepad.runMenuCommand( 'Macro','line join')
else:
pass
i=i+1
print 'done'
Notpad.save()
This does not work. 这不起作用。 How can I fix it? 我该如何解决?
As an alternative to using Notepad++, you could just use Python directly to modify the input file: 作为使用Notepad ++的替代方法,您可以直接使用Python来修改输入文件:
with open('input.txt', 'r') as f_input:
text = f_input.read()
text = re.sub(r'(^.*?(?=\n\S+|\Z))', lambda x: re.sub(r"(\n\s+)", " ", x.group(1)), text, flags=re.M+re.S)
with open('input.txt', 'w') as f_output:
f_output.write(text)
This would give you the following output: 这会给你以下输出:
A a1,asdf,lkjhj some thing here
B MORE THINGS,HERE
C MORE TEXTS HERE HERE ALSO TEXTS AND SO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.