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

2024-10-03 17:23:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在'bulk\u data'var中有一个bulk数据,现在需要在sub var中找到并复制它,如下所示,如何用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'

还有没有研究手册来处理这种类型的数据操作?你知道吗


Tags: rminmodwithlinespacebulkstart
3条回答

为什么是你?看起来所有的东西都被不同的字符分开了。你知道吗

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

。。。你知道吗

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)

相关问题 更多 >