Shreya
Shreya

Reputation: 649

Get space after certain pattern

I have input file as

cellabc
A1
A2
Z
cellbpc
A1
A2
A3
Z

I want my output as

module cell abc
A1
A2
Z
module cell bpc
A1
A2
A3
Z

I tried code

for line in f:
re.sub(r'(cell)',r'\1 ',line)

Here I first tried to get space after cell word. But unable to get. Please correct me.

Upvotes: 1

Views: 52

Answers (1)

gmdev
gmdev

Reputation: 3155

You can try this:

for line in f:
    sub = re.sub('cell', 'module cell ', line)

re.sub takes three obligatory arguments:

re.sub(pattern, repl, string)

Where the pattern is what you're looking for, the replacement of the string, and string that remains when the found pattern is removed.

module cell abc
A1
A2
Z
module cell bpc
A1
A2
A3
Z

Upvotes: 1

Related Questions