Python Forum
Removing hyphens and adding zeros
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing hyphens and adding zeros
#3
There is string method zfill for filling in zeros and if the objective is to have standard lenght (9) and fill shorter values with zeros and removing hyphens one can simply do:

>>> ssns = ['867-53-0909','333-22-4444','123-12-1234', '777-93-9311','123-12-1423', '1-2-3', 'Missing', '45-67-78']
>>> [''.join(ssn.split('-')).zfill(9) for ssn in ssns if ssn != 'Missing']
['867530909',
 '333224444',
 '123121234',
 '777939311',
 '123121423',
 '000000123',
 '000456778']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Removing hyphens and adding zeros - by sidney - Aug-14-2019, 09:18 AM
RE: Removing hyphens and adding zeros - by ThomasL - Aug-14-2019, 12:31 PM
RE: Removing hyphens and adding zeros - by perfringo - Aug-14-2019, 01:28 PM
RE: Removing hyphens and adding zeros - by sidney - Aug-14-2019, 01:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  getting trailing zeros with 1 during pandas read fullstop 1 3,559 Jan-05-2020, 04:01 PM
Last Post: ichabod801
  How to use Scipy.special to retrieve complex Poles / Zeros of Bessel Polynominal Detzi 2 1,758 Dec-31-2019, 10:45 AM
Last Post: Detzi
  Numpy saving and loading introduces zeros in the middle of every element DreamingInsanity 0 1,417 Dec-11-2019, 07:21 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

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