简体   繁体   中英

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 . 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM