Jon Poler
Jon Poler

Reputation: 183

formatting a string where variable does not always exist in Python

In one of my projects, I'm trying to parse parcel numbers that sometimes do, and sometimes don't have lot extensions (a three digit code at the end). I could obviously make an if elif structure to handle cases where lot extensions aren't present, but I was hoping to satisfy my curiosity and get some feedback on more efficient ways to write the code.

In it's current state, I end up with an unwanted trailing dash on parcels without a lot extension: '00-000-0000-'

Final parcel number formats should be:

and the input pins look like:

pin_that_wont_work1 = '00000000'
pin_that_wont_work2 = '000000000'
pin_that_works1 =     '00000000000'
pin_that_works2 =     '000000000000'

import re

pattern = r'^(\d{1,2})(\d{3})(\d{4})(\d{3})?$'

def parse_pins(pattern, pin):
    L = [x for x in re.search(pattern, pin).groups()]
    return '{dist}-{map_sheet}-{lot}-{lot_ext}'.format(dist=L[0] if len(L[0]) == 2 else '0'+L[0],
                                                       map_sheet=L[1],
                                                       lot=L[2],
                                                       lot_ext=L[3] if L[3] else '')

Upvotes: 0

Views: 113

Answers (3)

Hugh Bothwell
Hugh Bothwell

Reputation: 56674

import re

pin_pattern = re.compile(r'^(\d{1,2})(\d{3})(\d{4})(\d{3})?$')

pin_formats = {
    3: '{0:02d}-{1:03d}-{2:04d}',
    4: '{0:02d}-{1:03d}-{2:04d}-{3:03d}'
}

def parse_pin(s):
    groups = [int(d) for d in pin_pattern.search(s).groups() if d is not None]
    return pin_formats[len(groups)].format(*groups)

Upvotes: 2

dstromberg
dstromberg

Reputation: 7187

Throw them in a list, and list_.join('-'). The list should have 3 or 4 values.

Upvotes: 2

red
red

Reputation: 704

Maybe I'm missing something, but couldn't you just put the dash inside the format call?

def parse_pins(pattern, pin):
    L = [x for x in re.search(pattern, pin).groups()]
    return '{dist}-{map_sheet}-{lot}{lot_ext}'.format(dist=L[0] if len(L[0]) == 2 else '0'+L[0],
                                                       map_sheet=L[1],
                                                       lot=L[2],
                                                       lot_ext='-{0}'.format(L[3]) if L[3] else '')

Upvotes: 2

Related Questions