简体   繁体   中英

Replacing every nth occurrence of a string by n + string in text file

A text file contains

This is line ABC XYZ. This is something. This is ABC XYZ. foo. This is ABC XYZ. foo

The required output is

This is line 1 ABC XYZ. This is something. This is 2 ABC XYZ. foo. This is 3 ABC XYZ. foo 

So the problem is to replace nth occurrence of ABC XYZ by n ABC XYZ .

You could use a list comprehension

a="This is line ABC XYZ. This is something. This is ABC XYZ. foo. This is ABC XYZ. foo"
''.join([e+str(c+1)+" ABC XYZ" for c,e in enumerate(a.split("ABC XYZ"))][0:-1])+a.split("ABC XYZ.")[-1]

The method re.sub can take a function as second argument. Use a stateful function with an itertools.count object as counter.

Code

import re, itertools

s = 'This is line ABC XYZ. This is something. This is ABC XYZ. foo. This is ABC XYZ. foo'

def enumerator():
    counter = itertools.count(1)

    return lambda m: '{} {}'.format(next(counter), m.group())

out = re.sub(r'ABC XYZ', enumerator(), s)

print(out)

The function enumerator can be reused for any pattern.

Output

This is line 1 ABC XYZ. This is something. This is 2 ABC XYZ. foo. This is 3 ABC XYZ. foo

Code :

import re

text = "This is line ABC XYZ. This is something. This is ABC XYZ. foo. This is ABC XYZ. foo"
x = re.split("(ABC XYZ)",text)
c=0
for i,s in enumerate(x):
    if re.match('(ABC XYZ)',x[i]):
        c+=1
        x[i] = str(c)+' '+x[i]
x = ''.join(x)   # This is line 1 ABC XYZ. This is something. This is 2 ABC XYZ. foo. This is 3 ABC XYZ. foo

You can use more optimized ways of doing this, however this would help you understand it better.

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