Mar-24-2023, 01:59 AM
Greetings to you all!
I'm trying to replace single digits at the beginning of each string in a files
The integers are always in the range of 1-12.
Here is a test file:
Thank you.
I'm trying to replace single digits at the beginning of each string in a files
The integers are always in the range of 1-12.
Here is a test file:
1,xxx,ddd-02,cc1, 2,xxx,ddd-02,cc1, 3,xxx,ddd-02,cc1, 10,xxx,ddd-02,cc1, 11,xxx,ddd-02,cc1 12,xxx,ddd-02,cc1,Here is a snippet:
with open(fls) as blt_orig : for el in blt_orig : el.strip() num,*gb = el.split(",") new_n = num.zfill(2) # <-- Adding a zero to the number nln =re.sub("\d|\d{2}",new_n,el,count=1) # <-- replacing the digits in the string with the new one print(f"{nln}")And a strange printout:
01,xxx,ddd-02,cc1, 02,xxx,ddd-02,cc1, 03,xxx,ddd-02,cc1, 100,xxx,ddd-02,cc1, 111,xxx,ddd-02,cc1 122,xxx,ddd-02,cc1,For some reason, it prints extra digits to the 10/11 and 12.
Thank you.