Mar-24-2023, 02:36 AM
(This post was last modified: Mar-24-2023, 02:36 AM by deanhystad.)
The probelm is in the re.sub which says "replace one or two digits in el with new_n. When el starts with 2 digits, both 1 digit and two digits match, and the first match wins. If you write the regex like this it works:
That works, but it is an odd way to solve the problem. You want to get the numeric prefix, and the rest of the string. You then want to pad the prefix length 2 and concatenate. I would not use regex or zfill.
nln = re.sub("\d{2}|\d", new_n, el, count=1)Now it says "replace two digits or one digit witn new_n.
That works, but it is an odd way to solve the problem. You want to get the numeric prefix, and the rest of the string. You then want to pad the prefix length 2 and concatenate. I would not use regex or zfill.
with open("input.txt") as f: for line in f: prefix, suffix = line.strip().split(",", maxsplit=1) print(f"{prefix:>02},{suffix}")