Python Forum
Python start from a specific string line and write?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python start from a specific string line and write?
#1
I want to match a specific string and once match start writing the next line after after the matched string.

from output save in x variable I want to match "Current configuration" then write then start writing from next line.

Heres the sample output sting.

show run
Building configuration...

Current configuration : 1505 bytes
!
Content1
Content1
!
Content2
Content2
Once matched write but starting from next line. heres the target output.
!
Content1
Content1
!
Content2
Content2
Sample Configuration(but string the for line and not matching):
str = """show run
Building configuration...

Current configuration : 1505 bytes
!
Content1
Content1
!
Content2
Content2"""

with open('test.txt', 'w') as x:
    print str
    if "Current configuration" in str:
        x.writelines(itertools.islice(str, 4, None))
Thanks
Reply
#2
It's never good idea to use built-ins as names in your code:

>>> str(12)
'12'
>>> str = 'something'
>>> str(12)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
From print statement I assume that Python 2.x is used. Is there any particular reason not to use Python 3?

Unfortunately I don't understand what you want to accomplish so no help here.

EDIT: it's tempting to think that now I understand what should be accomplished.

I see that itertools module is used and there is more suitable function: itertools.dropwhile()

from itertools import dropwhile

s = """show run
Building configuration...
 
Current configuration : 1505 bytes
!
Content1
Content1
!
Content2
Content2"""

iterator = dropwhile(lambda line: not line.startswith('Current'), s.split('\n')) 
next(iterator)
for line in iterator:
    print(line)
Output:
! Content1 Content1 ! Content2 Content2
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,392 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Start Putty into Python Form Schlazen 5 5,336 Dec-13-2022, 06:28 AM
Last Post: divya130
  Python Idle won't start totalmachine 9 3,382 Oct-16-2022, 05:57 PM
Last Post: totalmachine
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,306 Sep-27-2022, 01:38 PM
Last Post: buran
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,145 Jul-20-2022, 09:05 AM
Last Post: arbiel
  CSV to Text File and write a line in newline atomxkai 4 2,612 Feb-15-2022, 08:06 PM
Last Post: atomxkai
  readline.parse_and_bind() does not work in start-up script of Python interpreter zzzhhh 0 1,473 Jan-18-2022, 11:05 AM
Last Post: zzzhhh
  multi-line CMD in one-line python kucingkembar 5 3,862 Jan-01-2022, 12:45 PM
Last Post: kucingkembar
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,143 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  append a string to a modified line Mr_Blue 10 3,731 Sep-16-2021, 07:24 PM
Last Post: Mr_Blue

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020