Search code examples
pythonshort

Copy value in var from structured data


I have a bulk data in 'bulk_data' var, now need to find and copy it in sub var as per below, How to do it with python

bulk_data = """F0142514RM/JRSE1420 Mod/4758
F0144758RM/JRSE935 Mod/23
F014GS4RM/JRSE10 Mod/445
"""

typeA1 = <start with RM/>"JRSE1420"<until space> in 1st line
typeA2 = <start with RM/>"JRSE935"<until space> in 2nd line
typeA3 = <start with RM/>"JRSE10"<until space> in 3rd line

typeB1 = <start with space after typeA1>"Mod/4758"<until end of the line> in 1rd line
typeB2 = <start with space after typeA2>"Mod/23"<until end of the line> in 2nd line
typeB3 = <start with space after typeA3>"Mod/445"<until end of the line> in 3rd line


Overall result would be:
typeA1 = 'JRSE1420'
typeA2 = 'JRSE935'
typeA3 = 'JRSE10'

typeB1 = 'Mod/4758'
typeB2 = 'Mod/23'
typeB3 = 'Mod/445'

And also is there any study manual to deal with such type of data manipulation ?


Solution

  • You can use the re module

    import re
    
    bulk_data = '''F0142514RM/JRSE1420 Mod/4758
    F0144758RM/JRSE935 Mod/23
    F014GS4RM/JRSE10 Mod/445
    '''
    
    ptrn1 = re.compile(r'''
        ^       #matches the start of the string
        .*      #matches 0 or more of anything
        RM\/    #matches "RM" followed by "/"
        (\w+)   #matches one or more alphanumeric character and the undescore
        \b      #matches empty string
        .*      #matches anything
        $       #matches the end of string
    ''', re.MULTILINE | re.VERBOSE)
    
    ptrn2 = re.compile(r'''
        ^       #matches the start of the string
        .*      #matches 0 or more of anything
        \s      #matches a space character
        (Mod.*) #matches "Mod" follow by 0 or more of anything
        $       #matches the end of string
    ''', re.MULTILINE | re.VERBOSE)
    
    typeA1, typeA2, typeA3 = ptrn1.findall(bulk_data)
    typeB1, typeB2, typeB3 = ptrn2.findall(bulk_data)