Python Forum
zfill prints extra et the end of a var
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
zfill prints extra et the end of a var
#1
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:
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.
Reply


Messages In This Thread
zfill prints extra et the end of a var - by tester_V - Mar-24-2023, 01:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Zfill Method Parameter Confusion new_coder_231013 3 2,046 Dec-05-2022, 05:52 PM
Last Post: new_coder_231013
  variable prints without being declared. ClockPillow 2 2,672 Jul-11-2021, 12:13 AM
Last Post: ClockPillow
  Output prints Account.id at the end? LastStopDEVS 5 4,086 Dec-19-2020, 05:59 AM
Last Post: buran
  Try/Exept prints only ones tester_V 11 5,808 Nov-03-2020, 02:38 AM
Last Post: tester_V
  loop only prints last character. mcmxl22 1 2,594 Feb-17-2020, 02:36 AM
Last Post: menator01
  can you understand why this code prints None? arcbal 2 3,560 Mar-13-2019, 02:57 AM
Last Post: arcbal
  An Extra 'None' leoahum 5 5,383 Oct-18-2018, 08:20 PM
Last Post: volcano63
  What for a file that prints nothing sylas 1 2,791 Sep-12-2018, 01:18 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020