• Login
Community
  • Login

Automated Search & Replace help please

Scheduled Pinned Locked Moved General Discussion
3 Posts 2 Posters 311 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R
    Ramses505
    last edited by PeterJones Apr 8, 2022, 4:37 PM Apr 8, 2022, 4:20 PM

    I have a file where I want to do an extensive search and replace utilizing a subtitutions.txt file containing all the search terms and their replacements. The search terms often have spaces in their names like;

    User Logged Off.ico
    

    The replacement may also have multiple spaces like;

    User Logged Off 32.ico
    

    I have installed Python Script and have been trying to get a couple of scripts I found to work, but they use single word search and replace terms.

    This is the main script I have tried;

    with open('C:/Temp/Substitutions.txt') as f:
        for l in f:
            s = l.split()
            editor.replace(s[0], s[1])
    

    If I change the split seperator to (‘#’) it seems to work except I get and extra LF character after every replace which destroys the code.

    Notepad+ is such a brilliant product and I had not realised that you could use Python with it - it’s great that it has pushed me to try and get to grips with Python, even though it is proving frustrating at the monment.

    Any help much appreciated. Thanks for reading.

    –
    moderator added text block to make question clearer

    A 1 Reply Last reply Apr 8, 2022, 4:36 PM Reply Quote 0
    • A
      Alan Kilborn @Ramses505
      last edited by Apr 8, 2022, 4:36 PM

      @tobias-ray said in Automated Search & Replace help please:

      I get and extra LF character after every replace which destroys the code

      Try s = l.rstrip('\n\r').split('#')

      1 Reply Last reply Reply Quote 2
      • R
        Ramses505
        last edited by Apr 8, 2022, 4:45 PM

        @alan-kilborn said in Automated Search & Replace help please:

        s = l.rstrip(‘\n\r’).split(‘#’)

        Hi alan-kilborn, thanks so much for the help. In my simple test, that does solve the problem, appreciated.

        I will now give it a try for real and no doubt realise a few further issues.

        Thanks again.

        1 Reply Last reply Reply Quote 1
        1 out of 3
        • First post
          1/3
          Last post
        The Community of users of the Notepad++ text editor.
        Powered by NodeBB | Contributors