从结构化数据中复制 var 中的值

Copy value in var from structured data

我在 'bulk_data' 变量中有大量数据,现在需要按照下面的方法在子变量中查找并复制它,如何使用 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'

还有没有任何学习手册来处理这种类型的数据操作?

count = 1
li = []
with open('data') as f:
    for line in f:
        line = line.split()
        if line:
            a, b = line
            a = a[a.index('/')+1:]
            li.append("TypeA{} = {} ".format(count, a))
            li.append("TypeB{} = {} ".format(count, b))
            count += 1

for el in sorted(li):
    print(el)

TypeA1 = JRSE1420 
TypeA2 = JRSE935 
TypeA3 = JRSE10 
TypeB1 = Mod/4758 
TypeB2 = Mod/23 
TypeB3 = Mod/445

您可以使用 re 模块

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)

为什么要重新?看起来一切都已经用不同的字符正确分隔了。

lines = bulk_data.splitlines()
typeA1_, typeB1 = lines[0].split(' ')
typeA1 = typeA1_.split('/')[1]

...