I hopefully have an easy question. I just cant figure out the correct function to use. I want to replace repeating characters with different variables depending on how many times it repeats consecutively.
with open("text1.txt","r") as File:
for line in File:
Counting = line.count('a')
if Counting == 1:
Line1 = line.replace('a', '1')
elif Counting == 2:
Line1 = line.replace('aa', '2')
If you want to analyze/replace consecutive groups of letters, then perhaps itertools.groupby could be of interest. The example below first extracts all consecutive groups and then checks if the unique element in a particular group is
a. If so, it replaces this group with the corresponding count of elements, otherwise, it keeps the original input.
from itertools import groupby s = 'aaabaacdd' #test input ret = '' for k, v in groupby(s): chunk = list(v) cnt = len(chunk) if k == 'a': #the condition can be extended here, e.g., k == 'a' and cnt <= 9 #substitute the group of 'a's with something else #the substitution can take into account the number of consecutive #'a's stored in the variable cnt el = '%d' % (cnt) else: el = ''.join(chunk) ret += el print(ret)