[Python] What is the better method to parse these data instead of regex+conditional statements?

Discussão em 'Python' iniciado por Stack, Abril 28, 2025.

  1. Stack

    Stack Membro Participativo

    Suppose you have a data set that looks like

    name1: float

    name2: float [unit]

    name3: {str: float [unit], (...repeat)}

    name4: datetime

    name5: int

    name6: alphanumeric

    name7: strings (sentence/phrase)

    name8: {{...can contain any of above but we ignore..}}

    What is the appropriate parsing method from python? With regex I solve this partially but feels like I need additional hard coded if statements to get to a complete solution. I am looking for pointers or direction here. Something similar to pd.read_csv(...), perhaps with some pre-clean then throw it into a parser?

    PATTERN_GENERIC = r'(?P<name>[^,:]*)(,|:)\s?(?P<content>.*)'
    PATTERN_VALUE = r'(?P<name>[^:]*)[^0-9-]*(?P<content>[-+]?\d*\.\d+|\d+)'
    header = {}
    with open(f) as fopen:
    for line in fopen:
    # parse number only
    results = re.search(PATTERN_VALUE,line.strip('\n'))
    if results:
    h = results.groupdict()
    header[h['name'].replace(' ','')] = float(h['content'])
    else: #treat as ID
    results = re.search(PATTERN_GENERIC,line.strip('\n'))
    if results:
    h = results.groupdict()
    header[h['name'].replace(' ','')] = h['content']


    What I wish to achieve

    col1 | col2

    name | numbers

    name | non-numbers

    Currently I haven't write something to handle the {...} lines, I can do that with a specific if statement. Also, it falls short on whenever there is a number it is seen as a number which isn't true for alphanumeric case (they are IDs not values). To solve that I probably have to write a pattern to differentiate alphanumeric vs numbers.

    Continue reading...

Compartilhe esta Página