簡體   English   中英

反向使用Python格式字符串進行解析

[英]Use Python format string in reverse for parsing

我一直在使用以下python代碼將整數部件ID格式化為格式化的部件號字符串:

pn = 'PN-{:0>9}'.format(id)

我想知道是否有一種方法可以反向使用相同的格式字符串( 'PN-{:0>9}' )來從格式化的部件號中提取整數ID。 如果無法做到,有沒有辦法使用單個格式字符串(或正則表達式?)來創建和解析?

解析模塊 “與format()”相反。

用法示例:

>>> import parse
>>> format_string = 'PN-{:0>9}'
>>> id = 123
>>> pn = format_string.format(id)
>>> pn
'PN-000000123'
>>> parsed = parse.parse(format_string, pn)
>>> parsed
<Result ('123',) {}>
>>> parsed[0]
'123'

你可能會發現模擬scanf interresting。

怎么樣:

id = int(pn.split('-')[1])

這將拆分破折號處的零件號,獲取第二個組件並將其轉換為整數。

PS我保留id作為變量名稱,以便與您的問題的連接清晰。 重命名該變量不會影響內置函數是個好主意。

如果您不想使用解析模塊,這是一個解決方案。 它將格式字符串轉換為具有命名組的正則表達式。 它做了一些假設(在docstring中描述)在我的情況下是可以的,但在你的情況下可能不合適。

def match_format_string(format_str, s):
    """Match s against the given format string, return dict of matches.

    We assume all of the arguments in format string are named keyword arguments (i.e. no {} or
    {:0.2f}). We also assume that all chars are allowed in each keyword argument, so separators
    need to be present which aren't present in the keyword arguments (i.e. '{one}{two}' won't work
    reliably as a format string but '{one}-{two}' will if the hyphen isn't used in {one} or {two}).

    We raise if the format string does not match s.

    Example:
    fs = '{test}-{flight}-{go}'
    s = fs.format('first', 'second', 'third')
    match_format_string(fs, s) -> {'test': 'first', 'flight': 'second', 'go': 'third'}
    """

    # First split on any keyword arguments, note that the names of keyword arguments will be in the
    # 1st, 3rd, ... positions in this list
    tokens = re.split(r'\{(.*?)\}', format_str)
    keywords = tokens[1::2]

    # Now replace keyword arguments with named groups matching them. We also escape between keyword
    # arguments so we support meta-characters there. Re-join tokens to form our regexp pattern
    tokens[1::2] = map(u'(?P<{}>.*)'.format, keywords)
    tokens[0::2] = map(re.escape, tokens[0::2])
    pattern = ''.join(tokens)

    # Use our pattern to match the given string, raise if it doesn't match
    matches = re.match(pattern, s)
    if not matches:
        raise Exception("Format string did not match")

    # Return a dict with all of our keywords and their values
    return {x: matches.group(x) for x in keywords}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM